我正在尝试使用PowerShell脚本打开IE并登录到Intranet站点。该脚本将打开IE并加载页面,但我收到以下错误:
You cannot call a method on a null-valued expression. At D:\Admin Programs\Bartender\Print Portal Logon Scripts\DahliaPPLogonv2.ps1:9 char:1 + $usernamefield = $ie.Document.getElementByID('UserName') + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull The property 'value' cannot be found on this object. Verify that the property exists and can be set. At D:\Admin Programs\Bartender\Print Portal Logon Scripts\DahliaPPLogonv2.ps1:10 char:1 + $usernamefield.value = 'jbob' + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : PropertyNotFound
每个变量的错误重复,我无法弄清楚原因。完整的脚本如下。
# Create an Internet Explorer object
$ie = New-Object -ComObject 'InternetExplorer.Application'
$ie.Visible= $true # Make it visible
# Open website
$ie.Navigate("http://bws01/BarTender/Account/Login")
#Wait till webpage is loaded
while ($ie.Busy -eq $true) {Start-Sleep -Seconds 5;}
# Feed credentials to the input fields on the web page
$usernamefield = $ie.Document.getElementByID('UserName')
$usernamefield.value = 'jbob'
$passwordfield = $ie.Document.getElementByID('Password')
$passwordfield.value = "123456"
$chk = $ie.Document.getElementById("RememberMe").Checked = $true
$submit = $ie.Document.getElementById("LogonPageSubmitButton").Click()