I'm trying to use a PowerShell script to open IE and login to an intranet site. The script will open IE and load the page but I am getting the following error:
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
The errors repeat for each variable and I cannot figure out why. Full script is below.
# 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()