powershell

PowerShell : Http Get/Post

This is another article in my on going learning/experimenting with PowerShell. This time I will show how you can use PowerShell to carry out REST operations such as GET/POST.

Now there may be some amongst you, who go why didn’t you just use WGet, which is a downloadable thing, and I could have indeed used that, except for the part that the machine I need to run this on, is so locked down that I can only use things that are already installed. So raw PowerShell it is

Here is the relevant PowerShell code, which allows 3 parameters to control the script

  • Target : The url
  • Verb : This is the http verb, GET, PUT etc
  • Content : This could be some content when doing a POST request for example

It just makes use of some very standard .NET classes namely WebRequest

[CmdletBinding()]
Param(
 
 
    [Parameter(Mandatory=$True,Position=1)]
    [string] $target,
 
    [Parameter(Mandatory=$True,Position=2)]
    [string] $verb,      
 
    [Parameter(Mandatory=$False,Position=3)]
    [string] $content
 
)
 
 
write-host "Http Url: $target"
write-host "Http Verb: $verb"
write-host "Http Content: $content"
 
 
 
$webRequest = [System.Net.WebRequest]::Create($target)
$encodedContent = [System.Text.Encoding]::UTF8.GetBytes($content)
$webRequest.Method = $verb
 
write-host "UTF8 Encoded Http Content: $content"
if($encodedContent.length -gt 0) {
    $webRequest.ContentLength = $encodedContent.length
    $requestStream = $webRequest.GetRequestStream()
    $requestStream.Write($encodedContent, 0, $encodedContent.length)
    $requestStream.Close()
}
 
[System.Net.WebResponse] $resp = $webRequest.GetResponse();
if($resp -ne $null) 
{
    $rs = $resp.GetResponseStream();
    [System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs;
    [string] $results = $sr.ReadToEnd();
 
    return $results 
}
else
{
    exit ''
}

Here is an example usage (assuming the above script is saved as http.ps1 somewhere):

Http.ps1 -target “http://www.google.com” -verb “GET”

Which would give the following sort of output:

image

Anyway that’s it for now, hope this helps

6 thoughts on “PowerShell : Http Get/Post

  1. Hello Sacha, thank you very much for this part of code. It is hard to find any hint how to natively implement wget in PowerShell 2.0 environment.
    However the proposed code did not work properly for me as the web server (Apache 2.2.3) always returned blank $_POST array when using POST method with POST data in $content variable.
    After looking at native wget debug output comparing to PowerShell $webRequest object properties I realize that I need to set the property of $webRequest.Content type to ‘application/x-www-form-urlencoded”, as this Property is blank by default.

  2. Hi, Sacha, how can I retrieve the Web Certificate via powershell, because I searched in forums but I got stucked. I made a script to retrieve some info about local certificates, but I need to target the Web ones.

    $machine = “hostname”
    foreach ($loop in $machine){
    $store = New-Object System.Security.Cryptography.X509Certificates.X509Store(“\\$machine\My”,”LocalMachine”)
    $store.Open(“ReadOnly”)
    $dayalert = 500
    $deadline = (Get-Date).AddDays($dayalert)
    $query = $store.certificates | % {
    If ($_.NotAfter -lt $deadline) {
    $_ | Select Thumbprint,Issuer, Subject, NotAfter, @{Label=”ExpiresIn”; Expression={($_.NotAfter – (Get-Date)).Days}}
    }
    }
    $cadday = $query | select-object -property subject,notafter | out-string
    $cadday
    $time = Get-Date -DisplayHint Time
    $from = “gbotet@araba.eus”
    $to = “gbotet@araba.eus”
    $subject = “Aviso de caducidad de certificados”
    $smtpserver = “smtp.dfa.es”

    foreach ($recipient in $to){write-host “Enviando mail a $to” | Send-MailMessage -smtpServer “smtp.dfa.es” -from $from -to $recipient -subject $subject -body $cadday}
    }

Leave a comment