使用Powershell获取当前登录的网络会话cookie



我目前使用浏览器登录,并希望获取该会话的当前cookie。 但是当我使用此代码时,它仅为该请求创建另一个会话 ID。不适用于我的浏览器中当前登录的会话。

$url = "http://example.website/" 
$cookiejar = New-Object System.Net.CookieContainer 
$webrequest = [System.Net.HTTPWebRequest]::Create($url); 
$webrequest.CookieContainer = $cookiejar 
$response = $webrequest.GetResponse() 
$cookies = $cookiejar.GetCookies($url) 
foreach ($cookie in $cookies) { 
Write-Host "$($cookie.name) = $($cookie.value)" 
}

我想在我的浏览器和脚本中输出类似的会话 ID cookie。

正如Lee_Dailey所建议的,您可以使用IE COM对象接口,但是cookie详细信息是有限的。详细信息以字符串形式返回,可以转换为哈希表以获取键,值对 - 但域或过期等扩展信息将不可用。

这仅适用于Internet Explorer,我不确定信息有多完整,例如是否可以以这种方式检索安全cookie,因此您需要进行测试。

根据您的要求,这可能足够,也可能不够。

#URL we want to retrieve cookie information from
$URL = 'https://stackoverflow.com'
#Hashtable to store the cookie details
$CookieDetails = @{}
#Create a shell object
$Shell = New-Object -ComObject Shell.Application
#Find the web browser tab that starts with our URL
$IE = $Shell.Windows() | Where-Object { $_.Type -eq "HTML Document" -and $_.LocationURL -like "$URL*"}
#Split the cookie string and for each line parse into k,v pairs 
foreach($Line in ($IE.Document.cookie -split "; "))
{
$Line = $Line -split "="
$CookieDetails.($Line[0]) = $Line[1..-1] -join "="
}
#Output the hashtable result
$CookieDetails

最新更新