Skip to content

Instantly share code, notes, and snippets.

@DanielSmon
Last active March 10, 2019 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielSmon/de5f8c3725366d57a674c868a3a259ae to your computer and use it in GitHub Desktop.
Save DanielSmon/de5f8c3725366d57a674c868a3a259ae to your computer and use it in GitHub Desktop.
PowerShell web request with cookies
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$uri = "https://www.domain.com/action"
# Cookies from browser
# Paste in cookie values from browser request
$allCookies = "cookie1=cookievalue1; cookie2=cookievalue2"
foreach ($cookiePair in $allCookies.Split((";"))) {
$cookieValues = $cookiePair.Trim().Split("=")
$cookie = New-Object System.Net.Cookie
$cookie.Name = $cookieValues[0]
$cookie.Value = $cookieValues[1]
$cookie.Domain = ([System.Uri]$uri).Host
$session.Cookies.Add($cookie);
}
$test = Invoke-WebRequest -Method Get `
-WebSession $session `
-Uri $uri
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment