- cls
- function basic()
- {
- #preemtive authentication
- $uri = New-Object System.Uri ($baseuri+"basic-auth/" + $username + "/" + $password)
- $encoded = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($username+":"+$password ))
- $headers = @{Authorization = "Basic "+$encoded}
- $r = Invoke-WebRequest -Uri $uri.AbsoluteUri -Headers $headers
- $r.Content
- }
- function basic2()
- {
- #server will respond with 401 on first attempt, then Powershell will add the credentials and retry
- $uri = New-Object System.Uri ($baseuri+"basic-auth/" + $username + "/" + $password)
- $secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
- $mycreds = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)
- $r = Invoke-WebRequest -Uri $uri.AbsoluteUri -Credential $mycreds
- $r.Content
- }
- function digest()
- {
- #server will respond with 401 on first attempt, then Powershell will add the credentials
- #and retry based on header info from first attempt
- $uri = New-Object System.Uri ($baseuri+"digest-auth/auth/" + $username + "/" + $password)
- $secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
- $mycreds = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)
- $r = Invoke-WebRequest -Uri $uri.AbsoluteUri -Credential $mycreds
- $r.Content
- }
- function response()
- {
- #Make server send back a custom header
- $myheader = "myheader"
- $myvalue = "myvalue"
- $uri = New-Object System.Uri ($baseuri+"response-headers")
- $headers = @{$myheader = $myvalue}
- $r = Invoke-WebRequest -Uri $uri.AbsoluteUri -Body $headers
- $r.Headers
- }
- function ip()
- {
- #returns IP address of calling client
- $uri = New-Object System.Uri ($baseuri+"ip")
- $r = Invoke-WebRequest -Uri $uri.AbsoluteUri
- $parsed = ConvertFrom-Json -InputObject $r.Content
- $parsed.origin
- }
- function headers()
- {
- #httpbin will in this case return headers as payload
- $uri = New-Object System.Uri ($baseuri+"headers")
- $r = Invoke-WebRequest -Uri $uri.AbsoluteUri
- $parsed = ConvertFrom-Json -InputObject $r.Content
- $parsed.headers
- }
- function setcookies()
- {
- #receive Set-Cookies header from servver
- $cookiename = "mycookie"
- $cookievalue = "myvalue"
- $cookies = @{$cookiename = $cookievalue}
- $uri = New-Object System.Uri ($baseuri+"cookies/set")
- $r = Invoke-WebRequest -Uri $uri.AbsoluteUri -Body $cookies
- $session.Cookies.GetCookies($uri) | Select-Object Name,Value
- }
- function getcookies()
- {
- #check that we send cookies correctly
- $cookiename1 = "mycookie"
- $cookievalue1 = "myvalue"
- $cookiename2 = "anothercookie"
- $cookievalue2 = "anothervalue"
- $cookies = @{$cookiename1 = $cookievalue1; $cookiename2 = $cookievalue2}
- Add-Type -AssemblyName Microsoft.PowerShell.Commands.Utility
- #Cookies
- $uri = New-Object System.Uri ($baseuri+"cookies")
- $cc = New-Object System.Net.CookieContainer
- foreach ($c in $cookies.Keys)
- {
- $cookie = New-Object System.Net.Cookie $c, $cookies[$c], $uri.AbsolutePath, $uri.Host
- $cc.Add($cookie)
- }
- #Sessionvariable
- $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
- $session.Cookies = $cc
- $r = Invoke-WebRequest -Uri $uri.AbsoluteUri -WebSession $session
- $session.Cookies.GetCookies($uri) | Select-Object Name,Value
- #change cookievalue and send it back
- $session.Cookies.GetCookies($uri)[$cookiename1].Value = "valuehaschanged"
- $r = Invoke-WebRequest -Uri $uri.AbsoluteUri -WebSession $session
- $session.Cookies.GetCookies($uri) | Select-Object Name,Value
- }
- function post()
- {
- #sends data with http post
- $uri = New-Object System.Uri ($baseuri+"post")
- $postvars = @{post1 = "value1";post2 = "value2";post3 = "value3";}
- #or $postvars = "post1=value1&post2=value2&post3=value3"
- $r = Invoke-WebRequest -Uri $uri.AbsoluteUri -Method Post -Body $postvars
- $parsed = ConvertFrom-Json -InputObject $r.Content
- $parsed.form
- }
- function get()
- {
- #sends data with http get
- $uri = New-Object System.Uri ($baseuri+"get?get1=value1&get2=value2&get3=value3")
- $r = Invoke-WebRequest -Uri $uri.AbsoluteUri
- $parsed = ConvertFrom-Json -InputObject $r.Content
- $parsed.args
- }
- function get2()
- {
- #sends data with http get
- $uri = New-Object System.Uri ($baseuri+"get")
- $getvars = @{get1 = "value1";get2 = "value2";get3 = "value3";}
- $r = Invoke-WebRequest -Uri $uri.AbsoluteUri -Body $getvars
- $parsed = ConvertFrom-Json -InputObject $r.Content
- $parsed.args
- }
- function status([int16] $status)
- {
- $uri = New-Object System.Uri ($baseuri+"status/"+$status.ToString())
- $r = Invoke-WebRequest -Uri $uri.AbsoluteUri
- $r.Content
- }
- function useragent([string] $ua)
- {
- $uri = New-Object System.Uri ($baseuri+"user-agent")
- $r = Invoke-WebRequest -Uri $uri.AbsoluteUri -UserAgent $ua
- $parsed = ConvertFrom-Json -InputObject $r.Content
- $parsed | Select-Object user-agent
- }
- $username = "myuser"
- $password = "mypass"
- $useragent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0"
- #httpbin(1): HTTP Request & Response Service
- $baseuri = "http://www.httpbin.org/"
- #useragent $useragent
- #post
- #get
- #get2
- #getcookies
- #setcookies
- #headers
- #basic
- #basic2
- #status 404
- #digest
- #response
- #ip
tirsdag 11. september 2012
Powershell v3.0: Invoke-WebRequest
With Powershell v3.0, a handy cmdlet Invoke-WebRequest is available. Kind of like curl/wget for PowerShell. Here are some examples on how to use. I have used httpbin.org which is excellent for testing request and server response: http://www.httpbin.org/
Etiketter:
auth,
basic,
cookiecontainer,
cookies,
credentials,
digest,
http,
httpbin,
invoke-webrequest,
json,
post,
powershell,
session
Abonner på:
Legg inn kommentarer (Atom)
Nicely done, am using for my scripting :-).
SvarSlettyou should blog some more. you have a talent.
SvarSlettDoes this code also work for a site that is authenticated using any SSO webagent?
SvarSlett