PoshCode Logo PowerShell Code Repository

SSL Oblivious Web Client by Carter Shanklin 5 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/624"></script>download | new post

This function creates a web client that will ignore all SSL certificate errors. Useful for uploading (HTTP PUT, maybe POST as well) to an https web server using a self-signed cert.

This function comes from Stephen Campbell of Marchview Consultants Ltd.

  1. function New-TrustAllWebClient {
  2.         # Create a compilation environment
  3.         $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
  4.         $Compiler=$Provider.CreateCompiler()
  5.         $Params=New-Object System.CodeDom.Compiler.CompilerParameters
  6.         $Params.GenerateExecutable=$False
  7.         $Params.GenerateInMemory=$True
  8.         $Params.IncludeDebugInformation=$False
  9.         $Params.ReferencedAssemblies.Add("System.DLL") > $null
  10.         $TASource=@'
  11.           namespace Local.ToolkitExtensions.Net.CertificatePolicy {
  12.             public class TrustAll : System.Net.ICertificatePolicy {
  13.               public TrustAll() {
  14.               }
  15.               public bool CheckValidationResult(System.Net.ServicePoint sp,
  16.                 System.Security.Cryptography.X509Certificates.X509Certificate cert,
  17.                 System.Net.WebRequest req, int problem) {
  18.                 return true;
  19.               }
  20.             }
  21.           }
  22. '@
  23.         $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
  24.         $TAAssembly=$TAResults.CompiledAssembly
  25.  
  26.         ## We now create an instance of the TrustAll and attach it to the ServicePointManager
  27.         $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
  28.         [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
  29.  
  30.         ## The ESX Upload requires the Preauthenticate value to be true which is not the default
  31.         ## for the System.Net.WebClient class which has very simple-to-use downloadFile and uploadfile
  32.         ## methods.  We create an override class which simply sets that Preauthenticate value.
  33.         ## After creating an instance of the Local.ToolkitExtensions.Net.WebClient class, we use it just
  34.         ## like the standard WebClient class.
  35.         $WCSource=@'
  36.           namespace Local.ToolkitExtensions.Net {
  37.             class WebClient : System.Net.WebClient {
  38.               protected override System.Net.WebRequest GetWebRequest(System.Uri uri) {
  39.                 System.Net.WebRequest webRequest = base.GetWebRequest(uri);
  40.                 webRequest.PreAuthenticate = true;
  41.                 webRequest.Timeout = 10000;
  42.                 return webRequest;
  43.               }
  44.             }
  45.           }
  46. '@
  47.         $WCResults=$Provider.CompileAssemblyFromSource($Params,$WCSource)
  48.         $WCAssembly=$WCResults.CompiledAssembly
  49.  
  50.         ## Now return the custom WebClient. It behaves almost like a normal WebClient.
  51.         $WebClient=$WCAssembly.CreateInstance("Local.ToolkitExtensions.Net.WebClient")
  52.         return $WebClient
  53. }
  54.  
  55. # Example of using this function to upload a file over SSL.
  56. # Notice that the object you get back from New-TrustAllWebClient is almost identical
  57. # to what you would get from new-object system.net.webclient.
  58. # $wc = New-TrustAllWebClient
  59. # $credential = get-credential
  60. # $wc.set_Credentials($credential.GetNetworkCredential())
  61. # $URL = "https://192.168.25.129/folder/VM%201/VM%201.vmx?dcPath=ha-datacenter&dsName=datastore1"
  62. # $wc.UploadString($URL, "PUT", "Testing")

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me