Created
September 20, 2017 12:26
-
-
Save someshinyobject/651ad4e854e5ce13ac3042cd29b28962 to your computer and use it in GitHub Desktop.
A proxy command of PowerShell's New-WebServiceProxy to allow for the passing of client certificates during creation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function New-ProxyWebServiceProxy { | |
[CmdletBinding(DefaultParameterSetName='NoCredentials', HelpUri='http://go.microsoft.com/fwlink/?LinkID=135238')] | |
Param ( | |
[Parameter(Mandatory=$true, Position=0)] | |
[Alias('WL','WSDL','Path')] | |
[ValidateNotNullOrEmpty()] | |
[uri] | |
${Uri}, | |
[Parameter(ParameterSetName='Credential')] | |
[Alias('Cred')] | |
[ValidateNotNullOrEmpty()] | |
[pscredential] | |
[System.Management.Automation.CredentialAttribute()] | |
${Credential}, | |
[Alias('Cert')] | |
[ValidateNotNullOrEmpty()] | |
[System.Security.Cryptography.X509Certificates.X509Certificate2] | |
${Certificate}, | |
[Parameter(Position=1)] | |
[Alias('FileName','FN')] | |
[ValidateNotNullOrEmpty()] | |
[string] | |
${Class}, | |
[Parameter(Position=2)] | |
[Alias('NS')] | |
[ValidateNotNullOrEmpty()] | |
[string] | |
${Namespace}, | |
[Parameter(ParameterSetName='UseDefaultCredential')] | |
[Alias('UDC')] | |
[ValidateNotNull()] | |
[switch] | |
${UseDefaultCredential} | |
) | |
Begin { | |
Try { | |
$outBuffer = $null | |
If ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer)) { | |
$PSBoundParameters['OutBuffer'] = 1 | |
} | |
If ($Certificate) { | |
$TempFile = [System.IO.Path]::GetTempFileName() + "_wsdl.xml" | |
$WebRequest = @{ | |
"Certificate" = $Certificate | |
"URI" = $URI | |
} | |
If ($Credential) { | |
$WebRequest.Credential = $Credential | |
#$WebRequest.ProxyCredential = $Credential | |
} ElseIf ($UseDefaultCredential) { | |
$WebRequest.UseDefaultCredentials = $True | |
#$WebRequest.ProxyUseDefaultCredentials = $True | |
} | |
$Response = Invoke-WebRequest @WebRequest | |
$Content = [XML]$Response.Content | |
$FirstChild = $Content.FirstChild | |
If ($FirstChild.Name -eq "xml") { | |
$Content.RemoveChild($FirstChild) | Out-Null | |
} | |
$Content.OuterXml | Out-File -FilePath $TempFile | |
$PSBoundParameters.Uri = "file://" + $TempFile | |
} | |
$PSBoundParameters.Remove("Certificate") | Out-Null | |
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('New-WebServiceProxy', [System.Management.Automation.CommandTypes]::Cmdlet) | |
$scriptCmd = {& $wrappedCmd @PSBoundParameters } | |
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin) | |
$steppablePipeline.Begin($PSCmdlet) | |
} Catch { | |
Throw $_ | |
} | |
} | |
Process { | |
Try { | |
$Proxy = New-WebServiceProxy @PSBoundParameters | |
$Proxy.ClientCertificates.Add($Certificate) | Out-Null | |
$Proxy | |
} Catch { | |
Throw $_ | |
} | |
} | |
End { | |
Try { | |
If ($TempFile) { | |
Remove-Item $TempFile | |
} | |
} Catch { | |
Throw | |
} | |
} | |
<# | |
.ForwardHelpTargetName New-WebServiceProxy | |
.ForwardHelpCategory Cmdlet | |
#> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment