我的问题是,我正在使用invoke web-request登录到一个网站。我能够成功登录该网站。我的问题是,当我转到url时,它认为我没有登录。我使用同一会话变量登录并转到特定页面。
这是显示我已登录的屏幕截图,但是我无法访问网站的一部分来上传一些文件。
https://drive.google.com/open?id=18tr4aBxTkroQKl1E1SOAEyeJ70LH2LC0
$Global:organization = Read-Host "`tEnter the organization"
$username = Read-Host "`tEnter the username"
$password = Read-Host "`tEnter the password"
$form.fields["organization"] = $organization
$form.fields["Username"] = $username
$form.fields["Password"] = $password
$Global:loginResponse = Invoke-WebRequest -uri ("https://dealer.md-bmc.rpdss.com/" + $form.Action) -WebSession $login -Method post -Body $form.Fields
if ($loginResponse.ParsedHtml.title -eq "RAPID Dealer Reporting Site") {
Write-Host "`tLog in failed, please try again! `n" -ForegroundColor Red
}
else {
$info = $loginResponse.AllElements | where {$_.tagName -contains "span"}
$account = ($info.innerhtml | Select-Object -First 3).ToUpper()
Write-Host "`tLogged in as" $account "`n" -ForegroundColor Green
$upload = $loginResponse.AllElements.InnerHtml | Where-Object {$_ -match 'url="(administration/uploadprofileselector.aspx\?.*?)"'} |ForEach-Object {$matches[1]} | Select-Object -First 1
$uid = ($upload -replace '\D').Substring(0,4)
$oid = ($upload -replace '\D').Substring(4,6)
#this is where it "forgets" that it's already logged in.
$fileUploadurl = "https://dealer.md-bmc.rpdss.com/bwident/pawnshop/administration/uploadprofileselector.aspx?l=English&uid=$uid&oid=$oid"
$fileUploadPage = Invoke-WebRequest -Uri $fileUploadurl -WebSession $login
#this should be showing me a selection menu, but it's asking me to login again. Please see screenshot.
$fileUploadPage.ParsedHtml.body.innerText
}
答案 0 :(得分:0)
在第一次调用Invoke-WebRequest
时,您使用-SessionVariable
参数设置了会话变量:
$Global:loginResponse = Invoke-WebRequest -uri ("https://dealer.md-bmc.rpdss.com/" + $form.Action) -SessionVariable login -Method post -Body $form.Fields
(如您所见,变量名在这里没有$
前缀。您甚至可以像'login'
一样引用它,以使其更加清楚,它只是变量(字符串)的名称)
在随后的对Invoke-WebRequest
的调用中,您使用该会话变量和-WebSession
参数:
$fileUploadPage = Invoke-WebRequest -Uri $fileUploadurl -WebSession $login
p.s。如我前面的评论中所述,您还必须修复代码中的($upload -replace '\D')
,因为您现在正尝试将所有非数字字符替换为????