0
votes

I'm trying to login to IE using the below powershell script:

$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible

$password="password"

$ie.Navigate("URL")

While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}

$passwordfield = $ie.document.getElementByID('password')
$passwordfield.value = "$password"

$Link = $ie.document.getElementByID('Login')
$Link.click()

And below is the HTML code of my URL:

<form action="index.cfm?event=dashboard:config.index" method="post">
<h4>Password</h4>
<p>
    <input type="password" name="password" value="" size="20">&nbsp;
    <input type="submit" value="Login">
</p>

I get the below error message:

You cannot call a method on a null-valued expression. At line:14 char:1 + $Link.click() + ~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

1

1 Answers

0
votes

The problem is that you are trying to access an element in the code, using the function getElementByID. However, neither the password-input nor the submit button in your example code has an ID. So if the provided example HTML-code is correct, your script should be crashing on $passwordfield.value = "$password".

You can use other methods to get the correct field, look up GetElementsByName, or you can update the HTML-code to use IDs:

<p>
    <input type="password" id="password" name="password" value="" size="20">&nbsp;
    <input type="submit" id="submit" value="Login">
</p>