Powershell http post with .cer for auth

Pretty new to PowerShell and hoping someone can point me in the right direction.

I need to perform a POST request and have to pass it a locally stored cert (x509) during the POST request, for authentication.

What is the best way or way to accomplish this? I've found plenty of example to be able to perform this task in .net/C# but I am not finding anything that will accomplish this task in PowerShell.

Here is my POST request code, again I would like to point to a cert stored locally "C:\code\cert.crt" and pass it during the web transaction.

$url = "https://myUrl/uploadTester"
$data = '{"data": "988309487577839444"}'
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$b = [System.Text.Encoding]::ASCII.GetBytes($data)
$web = [System.Net.WebRequest]::Create($url)
$web.Method = "POST"
$web.ContentLength = $b.Length
$web.ContentType = "application/x-www-form-urlencoded"
$stream = $web.GetRequestStream()
$stream.Write($b,0,$b.Length)
$stream.close()

$reader = New-Object System.IO.Streamreader -ArgumentList $web.GetResponse().GetResponseStream()
$reader.ReadToEnd()
$reader.Close()

Thanks for all the help in advanced.

Answers


It's pretty easy to convert C# to PowerShell. Give this a try:

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$cert = [System.Security.Cryptography.X509Certificates.X509Certificate2]::CreateFromCertFile("C:\Users\Andy\Desktop\Test.cer")
$web = [System.Net.WebRequest]::Create($url)
$web.ClientCertificates.Add($Cert)

I adapted this from: http://support.microsoft.com/kb/895971

Looks like the key is the ClientCertificates property.

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.clientcertificates.aspx


Need Your Help

How to format a number into standard currency format (comma seperated digits)

java jquery format currency number-formatting

I want to know how can i format a number like 2000000 into standard currency format i.e 2,000,000 using Java code or jQuery ?

iPhone Memory Leaks

iphone objective-c memory-management memory-leaks

If an application produces a lot of memory leaks, are they "just" an in-app problem or are they also in RAM after the termination of the application? So does the iPhone OS release the memory alloca...