Invoke-webrequest登录&浏览

时间:2017-10-02 07:24:35

标签: powershell webrequest

我可以成功忽略证书警告并登录网站。但是,我无法浏览到另一个页面$奋斗。登录后我被困在主页面。有人知道如何使用相同的websession登录后从页面获取原始内容吗?感谢

[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

$myurl = "https://localhost/Portal";

$response = Invoke-WebRequest -Uri $myurl -Method Get -SessionVariable mySession

$form = $response.Forms[0]

$form.Fields["DBUser"] = "user"
$form.Fields["DBPassword"] = "pass"

$response = Invoke-WebRequest -Uri ($myurl + $form.Action) -WebSession $mySession -Method Post -Body $form.Fields

$response.statusDescription

$struggle = "https://localhost/Portal/Dashboard.aspx?selection=1001"

$pagedownload = Invoke-WebRequest -Uri ($struggle) -WebSession $mySession

1 个答案:

答案 0 :(得分:1)

我已根据我解决类似问题的方式调整了您的代码

[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

$myurl = "https://localhost/Portal";

$response = Invoke-WebRequest -Uri $myurl -Method Get

$form = $response.Forms[0]

$form.Fields["DBUser"] = "user"
$form.Fields["DBPassword"] = "pass"

$response = Invoke-WebRequest -Uri ($myurl + $form.Action) -SessionVariable mySession -Method Post -Body $form.Fields

$response.statusDescription

$struggle = "https://localhost/Portal/Dashboard.aspx?selection=1001"

$pagedownload = Invoke-WebRequest -Uri ($struggle) -SessionVariable mySession

不同之处在于我总是使用带有变量名称的-SessionVariable,因此不需要$。在获取我认为您在访问Invoke-WebRequest时要使用的Cookie /标头时,我还会在第二个$strugle上捕获您的会话。如果需要调整代码,在第一次调用时使用变量int。