Skip to content

Instantly share code, notes, and snippets.

@mwjcomputing
Last active December 12, 2015 01:08
Show Gist options
  • Save mwjcomputing/4688957 to your computer and use it in GitHub Desktop.
Save mwjcomputing/4688957 to your computer and use it in GitHub Desktop.
Adds a force switch to Invoke-RestMethod
function Invoke-RestMethod {
[CmdletBinding(HelpUri='http://go.microsoft.com/fwlink/?LinkID=217034')]
param(
[Microsoft.PowerShell.Commands.WebRequestMethod]
${Method},
[Parameter(Mandatory=$true, Position=0)]
[ValidateNotNullOrEmpty()]
[uri]
${Uri},
[Microsoft.PowerShell.Commands.WebRequestSession]
${WebSession},
[Alias('SV')]
[string]
${SessionVariable},
[pscredential]
${Credential},
[switch]
${UseDefaultCredentials},
[ValidateNotNullOrEmpty()]
[string]
${CertificateThumbprint},
[ValidateNotNull()]
[System.Security.Cryptography.X509Certificates.X509Certificate]
${Certificate},
[string]
${UserAgent},
[switch]
${DisableKeepAlive},
[int]
${TimeoutSec},
[System.Collections.IDictionary]
${Headers},
[ValidateRange(0, 2147483647)]
[int]
${MaximumRedirection},
[uri]
${Proxy},
[pscredential]
${ProxyCredential},
[switch]
${ProxyUseDefaultCredentials},
[Parameter(ValueFromPipeline=$true)]
[System.Object]
${Body},
[string]
${ContentType},
[ValidateSet('chunked','compress','deflate','gzip','identity')]
[string]
${TransferEncoding},
[string]
${InFile},
[string]
${OutFile},
[switch]
${PassThru},
[switch]
${Force})
begin
{
if ($force) {
[Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
}
try {
$outBuffer = $null
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
{
$PSBoundParameters['OutBuffer'] = 1
}
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Invoke-RestMethod', [System.Management.Automation.CommandTypes]::Cmdlet)
$scriptCmd = {& $wrappedCmd @PSBoundParameters }
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
} catch {
throw
}
}
process
{
try {
$steppablePipeline.Process($_)
} catch {
throw
}
}
end
{
try {
$steppablePipeline.End()
} catch {
throw
}
}
<#
.ForwardHelpTargetName Invoke-RestMethod
.ForwardHelpCategory Cmdlet
#>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment