Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用powershell获取当前登录的web会话cookie_Powershell_Cookies_Invoke Webrequest - Fatal编程技术网

使用powershell获取当前登录的web会话cookie

使用powershell获取当前登录的web会话cookie,powershell,cookies,invoke-webrequest,Powershell,Cookies,Invoke Webrequest,我当前使用浏览器登录,希望获取该会话的当前cookie。但当我使用这段代码时,它仅为该请求创建另一个会话id。不适用于我的浏览器中当前登录的会话 $url = "http://example.website/" $cookiejar = New-Object System.Net.CookieContainer $webrequest = [System.Net.HTTPWebRequest]::Create($url)

我当前使用浏览器登录,希望获取该会话的当前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。

正如建议的那样,您可以使用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

我认为这是不可能的,因为powershell和浏览器不共享同一个cookie存储。是否有方法使用powershell访问浏览器cookie存储?据我所知,cookie存储被设计为会话专用。我能想到的访问这些东西的唯一方法是。。。[A] 使用IE COM对象界面,或者[B]使用一些东西来自动化浏览器的使用,比如selenium。