Powershell to press a button in Explorer - powershell

I have a web page with the following code relevant to the button:
<td width=300 align=left>Click here to run the batch tool:</td>
<td align='left' width=100><input id="Submit1" type="submit" value="Run" onClick='showSts();'/></td>
And my attempts to programmatically press it from Powershell using this code:
$username='username' $password='password'
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true
$ie.Navigate("name of Web site")
while ($ie.Busy -eq $true){Start-Sleep -seconds 1;}
# $usernamefield = $ie.Document.getElementByID('memberemail')
# $usernamefield.value = $username
# $passwordfield = $ie.Document.getElementByID('memberpassword')
# $passwordfield.value = $password
$submitebutton=$ie.Documentelement.getElementByclassname('submit1') | Select-Object -First
$submitebutton.click()
Gives this error:
You cannot call a method on a null-valued expression. At
C:\Users\zkf4bi4\Documents\houdini_click.ps1:15 char:1
+ $submitebutton=$ie.Documentelement.getElementByclassname('submit1') | Select-Ob ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression. At
C:\Users\zkf4bi4\Documents\houdini_click.ps1:16 char:1
+ $submitebutton.click()
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Any ideas on how to proceed? I'm an experienced developer but am new to Powershell

Well, I can't help but notice that you're calling GetElementsByClassName but 'Submit1' is the elements id. I suspect the error message is trying to tell you that $submitebutton is null. You might find this question an almost exact duplicate.

Related

PowerShell doesn't show page after web login

This is the code:
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible
$username="name.surname#myemail.com"
$password="MyPassword"
$ie.Navigate("https://service.post.ch/zopa/dlc/app/?service=dlc-web&inMobileApp=false&inIframe=false&lang=fr#!/main")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
$usernamefield = $ie.document.getElementByID('isiwebuserid')
$usernamefield.value = "$username"
$passwordfield = $ie.document.getElementByID('isiwebpasswd')
$passwordfield.value = "$password"
$Link = $ie.document.getElementByID('actionLogin')
$Link.click()
while($ieobject.Busy) { Start-Sleep -seconds 10 }
$ie.Quit()
As you can see from the video the website post.ch opens up but then the window closes even if I set Start-Sleep -seconds 10 before $ie.Quit()
This makes me think that probably the script is working but not sure what happens in the next page.
The browser just closes.
Any idea what's going on?
You can also create an account on that website if you wan to test.
The error says:
Exception from HRESULT: 0x800A01B6
At C:\Users\fmv\Desktop\Import Ref Data scripts\getPosteDataUAT.ps1:12 char:1
+ $usernamefield = $ie.document.getElementByID('isiwebuserid')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], NotSupportedException
+ FullyQualifiedErrorId : System.NotSupportedException
The property 'value' cannot be found on this object. Verify that the property exists and can be set.
At C:\Users\fmv\Desktop\Import Ref Data scripts\getPosteDataUAT.ps1:13 char:1
+ $usernamefield.value = "$username"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
Exception from HRESULT: 0x800A01B6
At C:\Users\fmv\Desktop\Import Ref Data scripts\getPosteDataUAT.ps1:15 char:1
+ $passwordfield = $ie.document.getElementByID('isiwebpasswd')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], NotSupportedException
+ FullyQualifiedErrorId : System.NotSupportedException
The property 'value' cannot be found on this object. Verify that the property exists and can be set.
At C:\Users\fmv\Desktop\Import Ref Data scripts\getPosteDataUAT.ps1:16 char:1
+ $passwordfield.value = "$password"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
Exception from HRESULT: 0x800A01B6
At C:\Users\fmv\Desktop\Import Ref Data scripts\getPosteDataUAT.ps1:18 char:1
+ $Link = $ie.document.getElementByID('actionLogin')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], NotSupportedException
+ FullyQualifiedErrorId : System.NotSupportedException
You cannot call a method on a null-valued expression.
At C:\Users\fmv\Desktop\Import Ref Data scripts\getPosteDataUAT.ps1:19 char:1
+ $Link.click()
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
While ($ie.Busy -eq $true) is not a valid way to wait until a website is loaded. Unfortunately, there is no standard way of telling that a website is fully loaded. Javascript could load things at any time later, or permanently.
So you have to define your "fully loaded" state by yourself. For example, you could wait until a certain element exists.
Besides that, it looks like the website doesn't load in IE at all. So the elements will never be available in this browser (at least on my machine). It works in Edge and Firefox, though.
If the website loads in your IE, you can replace While ($ie.Busy -eq $true) ..., by just Start-Sleep -Seconds 10. So that you definitely wait 10 s. This should be enough for the elements to load (if they do it at all). And the errors should be gone. If that works, look for a better method to wait.

$ie.document method is not visible for console page in powershell

I am using powershell to automate login page but when i navigate to login console page there is no $ie.document method is visible. and showing the null error.
Method present for console page are :
CreateObjRef
Equals
GetHashCode
GetLifetimeService
GetType
InitializeLifetimeService
ToString
Code I am using is :-
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 7;}
$ie.Navigate("https://matlktwlsapp001:16311/ibm/console/")
start-sleep -s 2;
$ie | gm;
$qw = $ie.document.getElementById("overridelink");
$qw.click();
Error:-
You cannot call a method on a null-valued expression.
At line:7 char:1
+ $qw = $ie.document.getElementById("overridelink");
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:8 char:1
+ $qw.click();
+ ~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
Kindly Help!!

'Run As Different Users' behaves differently with PowerShell

I'm using "Run As Different User" when running a PowerShell script, and it is behaving differently than when I run the script in a normal PowerShell Terminal. For example, if I try to run the following code in the "Run As" terminal, it runs into an error. But if I run it with a normal terminal it works just fine.
function Replace-Word(
[string]$Document,
[string]$FindText,
[string]$ReplaceText
)
{
$ReplaceAll = 2
$FindContinue = 1
$MatchCase = $False
$MatchWholeWord = $True
$MatchWildcards = $False
$MatchSoundsLike = $False
$MatchAllWordForms = $False
$Forward = $True
$Wrap = $FindContinue
$Format = $False
$Word = New-Object -comobject Word.Application
$Word.Visible = $False
$OpenDoc = $Word.Documents.Open($Document)
$Selection = $Word.Selection
$Selection.Find.Execute(
$FindText,
$MatchCase,
$MatchWholeWord,
$MatchWildcards,
$MatchSoundsLike,
$MatchAllWordForms,
$Forward,
$Wrap,
$Format,
$ReplaceText,
$ReplaceAll
) | Out-Null
$OpenDoc.Close()
$Word.quit()
}
Copy-Item "C:\Welcome.DOC" "C:\test.DOC"
Replace-Word -Document "C:\test.DOC" -FindText '<UserName>' -ReplaceText "JohnDoe"
Replace-Word -Document "C:\test.DOC" -FindText '<EmailAddress>' -ReplaceText "JohnDoe#example.com"
The errors I get when running as a different user are:
You cannot call a method on a null-valued expression.
At C:\Users\lbradstr\Desktop\TechRepo\NewEEsetup\NewUserSetup.ps1:2668 char:9
+ $Selection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\lbradstr\Desktop\TechRepo\NewEEsetup\NewUserSetup.ps1:2670 char:9
+ $OpenDoc.Close()
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\lbradstr\Desktop\TechRepo\NewEEsetup\NewUserSetup.ps1:2668 char:9
+ $Selection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\lbradstr\Desktop\TechRepo\NewEEsetup\NewUserSetup.ps1:2670 char:9
+ $OpenDoc.Close()
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Both terminals are running the same version of PowerShell:
Major Minor Build Revision
----- ----- ----- --------
5 1 14409 1012
I would run this code in a normal terminal if I could, but this is only part of a larger script I am running. The rest of the script needs administrative privileges to run, which is why I use the "Run As" feature. Any ideas about why the code would execute differently between the two user accounts? Any suggestions would be very appreciated!
Everyone else's suggestion still apply (once permissions and ACL to the file has been sorted) and it is still giving you this error... THEN:
This is a classic case of locked file in Word. You need to release the COM Object from your last PowerShell session because it is probably still accessing the file and causing issues.
Once you release the COM Object or close all word processes, try running as again and it should work.

error "You cannot call a method on a null-valued expression" when selecting option from drop down list after logging into weblink using IE

I am trying to automate the IE link where I am logging into the webpage and selecting some dropdown option on that. I am able to login to the link but not able to select the option from dropdown.
while ($ie.Busy -eq $true -or $ie.Document -eq $null ){Start-Sleep -seconds 1;}
$dropDown = $ie.Document.getElementById("state-area").contentDocument.getElementById("state-status")[1].selected = $true
Write-Host("Trying to select the dropdown")
$dropDown = $ie.Document.getElementById("state-area").contentDocument.getElementById("state-status").FireEvent("Ready")
Output:
You cannot call a method on a null-valued expression.
At C:\Documents\TestScript4.ps1:51 char:13
+ $dropDown = $ie.Document.getElementById("state-area").contentDocument.getElement ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Trying to select the dropdown
You cannot call a method on a null-valued expression.
At C:\Documents\TestScript4.ps1:59 char:1
+ $dropDown = $ie.Document.getElementById("state-area").contentDocument.getElement ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Unable to login to a website via powershell

I am trying to open a url in IE and then pass username and password to login to the url.But my code is not filling username and password
Following is code
$username = "userhere"
$password = "passhere"
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$false
$ie.navigate("https://ameriprisestage.service-now.com/")
#while($ie.ReadyState -ne 4) {start-sleep -m 100}
$ie.document.getElementById("user_name").value= "$username"
$ie.document.getElementById("user_password").value = "$password"
Error: You cannot call a method on a null-valued expression. At line:7 char:1 + $ie.document.getElementById("user_name").value= "$username" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression. At line:8 char:1 + $ie.document.getElementById("user_password").value = "$password" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
There is a couple of things to note. First, your page is not ready yet. Yuo must wait for load. Your commented line was intended to do that. Second, both fields are in iframe which works as a separate sandbox - has his own document object. I also removed window hiding code to see results. Here is working code:
$username = "userhere"
$password = "passhere"
$ie = New-Object -com InternetExplorer.Application
$ie.navigate("https://ameriprisestage.service-now.com/")
while($ie.ReadyState -ne 4) {start-sleep -m 100}
$ie.document.getElementById('gsft_main').contentWindow.document.getElementById('user_name').value = $username
$ie.document.getElementById('gsft_main').contentWindow.document.getElementById('user_password').value = $password
Make sure you close $ie object. You may have tons of them as background processes.