PowerShell Url登录和状态检查



我正在尝试创建一个简单的脚本,该脚本登录到集合中的页面,并返回登录或不访问的确认,目前我可以成功登录,但我不知道如何获得它的确认。

错误类型不是我的观点,只有ACCESS或DENIED ACCESS 的代码


[array]$login = "<URL>", "<URL>"
$name = "marek"
$int = Read-Host "write password for user marek: " -AsSecureString
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($int)
$pass = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
foreach($url in $login) {

$ie = $null
$ie = New-Object -ComObject 'InternetExplorer.application'
$ie.Visible = $true
$ie.Navigate($url)
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 1;}
$namefield = $ie.document.getElementById("id_username")
$namefield.value = "$name"
$passfield = $ie.document.getElementById("id_password")
$passfield.value = "$pass"
$btn = $ie.document.getElementsByClassName('btn btn-success ml-2')
$btn[0].click()
#TODO
#receive confirmation of loggin or not

继续我的评论,关于,为什么不使用Invoke-*

MS文档

调用WebRequest调用RestMethod

例如:

(以下不是通用的,因为每个网站的用户名和密码输入都可以/将有不同的表单字段布局/分类法。(:

$UserCredentials = Get-Credential
$InvokeResponse  = Invoke-WebRequest $Url -SessionVariable MyInvokeSession
$form = $InvokeResponse.Forms
$form.fields['username'] = $UserCredentials.UserName
$form.fields['password'] = $UserCredentials.GetNetworkCredential().Password
$InvokeResponse = Invoke-WebRequest -Uri ($Url + $form.Action) -WebSession $MyInvokeSession -Method POST -Body $form.Fields

观看此Youtube视频,实时查看详细信息。。。

使用Invoke-WebRequest 登录网页

。。。并阅读本文中的详细信息。

使用PowerShell 进行Web刮擦

因此,根据我下面的评论,您可以使用上面的内容通过HTTP状态码检查成功/失败。。。

https://kinsta.com/blog/http-status-codes

。。。然后,如果这显示成功,你可以使用你的正常导航去那里。

最新更新