Here is the batch code:
<# :
#echo off
powershell /nop /ex bypass^
"&{[ScriptBlock]::Create((gc '%~f0') -join [Char]10).Invoke()}"
exit /b
#>
$ie = new-object -comobject InternetExplorer.Application
$ie.Visible = $false
$ie.Silent = $true
while($ie.Busy -ne $false) {start-sleep -m 100}
$ie.Navigate2("https://www.google.ru/", 4)
while($ie.Busy -ne $false) {start-sleep -m 100}
while($ie.ReadyState -ne 4) {start-sleep -m 100}
start-sleep -m 500
$ie.Stop()
echo $ie.Document.body.innerHTML > ".\html.txt"
$ie.Quit()
This code works fine in Windows 7 x64, Windows 8 x64, but not work in Windows 10 x64. Microsoft EDGE browser is not using, IE 11 is selected as default browser. I tried to run bat-file as administrator and as invoker.. What is wrong? Thanks for your help!
If you only need to retrieve the HTML content of a web page, don't use a web browser at all. Instead, use invoke-webrequest and access the content property of the resulting object.
invoke-webrequest -Uri http://www.google.ru/ | select-object -property content
Related
I am trying to log on web page using PowerShell and generate some. Although Powershell ISE gives some errors, current script works okay(sometimes log on sometimes not) but last part seem doesn't work to generate the Report by clicking the "CSV" Button.
Any help please?
$username = "username"
$password = "password"
$ie = New-Object -ComObject InternetExplorer.Application
$ie.visible=$true
$ie.navigate("http://..../login")
while($ie.ReadyState -ne 4) {start-sleep -m 300}
$ie.document.IHTMLDocument3_getElementById('user_session_email').Value=$username
$ie.document.IHTMLDocument3_getElementById('user_session_password').Value=$password
$ie.Document.IHTMLDocument3_getElementById('commit').Click()
$ie.navigate("http://..../activity_reports")
while($ie.ReadyState -ne 4) {start-sleep -m 100}
$currentDate= (Get-Date).ToString('dd-MM-yyyy')
$ie.document.IHTMLDocument3_getElementById('from_date').innertext= $currentDate
$ie.document.IHTMLDocument3_getElementById('to_date').Value=$currentDate
$Link=$ie.Document.IHTMLDocument3_getElementByType('submit') | where-object {$_.class -eq 'btn
btn-default btn-sm'}
$Link.click()
I am trying to download file on IE Version 21H from Website using powershell.
when I click Download button using powershell, it asks me for download Popup window with below massage.
You Want to Open or Save XYZ.log from www.XYZ.com
below is the code I am using
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible=$true
$ie.Navigate("www.xyz.com/ab/sder23445sdfrty") #please note this is random URL I provided
$link=$ie.Document.getElementsByTagName("Button") | where-object {$_.outerhtml -like "*download*"}
$link.click()
You can first active the IE window and bring it to front using AppActivate, then using SendKeys to send keystrokes Ctrl+S to save the file.
The sample code is like below, you can change the url and element selector to your owns:
[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible=$true
$ie.Navigate("https://www.example.com/download.html") #change it to your own url
while($ie.ReadyState -ne 4 -or $ie.Busy) {Start-Sleep -m 100}
$link=$ie.Document.getElementById("btnDowload") #change it to your own selector
$link.click()
Sleep 5
$ieProc = Get-Process | ? { $_.MainWindowHandle -eq $ie.HWND }
[Microsoft.VisualBasic.Interaction]::AppActivate($ieProc.Id)
[System.Windows.Forms.SendKeys]::Sendwait("%{s}");
I'm trying to log into my printers to grab logs from them and parse them with powershell.
The logon box isn't a FORM, it's an unordered list containing INPUT textboxes.
So I have managed to select the PIN textbox and give it the correct value.
I have also managed to get a hold of the button I have to click, but now my problem is, I need to click that button and declare a SessionVariable so I can continue with that session once the button is clicked and I'm logged in.
How do I properly click this button? I'm trying this but it's not working:
$Page = Invoke-WebRequest -Uri "http://xxx/webglue/content?c=LoginDropdown&lang=fr"`
-Headers #{"Pragma"="no-cache"; "Accept-Encoding"="gzip, deflate"; "Accept-Language"="fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7"; "User-Agent"="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"; "Accept"="application/json, text/javascript, */*; q=0.01"; "Cache-Control"="no-cache"; "X-Requested-With"="XMLHttpRequest"; "Cookie"="lang=fr; autoLogin=false"; "Referer"="http://xxx/"}`
-ContentType "application/x-www-form-urlencoded; charset=utf-8"`
-SessionVariable "SESSION"
$TextboxPIN = $page.ParsedHtml.body.getElementsByTagName('input') | Where-Object {$_.OuterHTML -match "pin"}
$TextboxPIN.value = "xxx"
$ConnectionButton = $page.ParsedHtml.body.getElementsByTagName('button') | Where-Object {$_.onclick -like "*login(this)*"}
Invoke-WebRequest -Uri ("http://xxx/webglue/content?c=LoginDropdown&lang=fr"+ $ConnectionButton.click() ) -Method POST -Body $ConnectionButton -SessionVariable "ADMIN"
This doesn't work for now because i'm not properly logged in since I can't click the button correclty with POST
Invoke-WebRequest -uri "http://xxx/cgi-bin/history" -WebSession $ADMIN
That http:// PRINTER-IP/webglue/content?c=LoginDropdown&lang=fr"+ $BoutonConnexion.click() is based on how it's done with forms but i'm sure it's wrong in this case. How to properly click it?
After trying a lot of different things, none worked, here is how I ended up getting it working. It's not pretty but it works well and takes just a few seconds to extract all the logs, which I can then parse later.
This is for a Lexmark MS621n printer but could be adapted to other Lexmark printers.
This dumps all the log files I found of interest:
$Printer = "IPofPrinter"
$ie = New-Object -ComObject 'InternetExplorer.Application'
$ie.visible = $true
$ie.navigate("http://$printer")
do {start-sleep -m 100} until ($ie.ReadyState -eq 4)
$Menu = $ie.document.IHTMLDocument3_getElementById("loginclickarea")
$menu.Click()
start-sleep 1
$pin = $ie.document.IHTMLDocument3_getelementsbytagname("input") | Where-Object {$_.OuterHTML -match "pin"}
$pin.focus()
$wshell = New-Object -ComObject wscript.shell
$wshell.SendKeys("XXXXX")
$btn = $ie.document.IHTMLDocument3_getelementsbytagname("button") | Where-Object {$_.OuterHTML -like "*login(this)*"}
$btn.Click()
do {start-sleep -m 100} until ($ie.ReadyState -eq 4)
$ie.navigate("http://$printer/cgi-bin/enginedebugdata")
do {start-sleep -m 100} until ($ie.ReadyState -eq 4)
$ie.Document.documentElement.innerHTML > C:\TEMP\$($printer)_enginedebugdata.log
$ie.navigate("http://$printer/cgi-bin/eventlog_se")
do {start-sleep -m 100} until ($ie.ReadyState -eq 4)
$ie.Document.documentElement.innerHTML > C:\TEMP\$($printer)_eventlog.log
$ie.navigate("http://$printer/cgi-bin/history")
do {start-sleep -m 100} until ($ie.ReadyState -eq 4)
$ie.Document.documentElement.innerHTML > C:\TEMP\$($printer)_history.log
$ie.navigate("http://$printer/cgi-bin/se_net_details")
do {start-sleep -m 100} until ($ie.ReadyState -eq 4)
$ie.Document.documentElement.innerHTML > C:\TEMP\$($printer)_se_net_details.log
$ie.navigate("http://$printer/cgi-bin/netsetuppg")
do {start-sleep -m 100} until ($ie.ReadyState -eq 4)
$ie.Document.documentElement.innerHTML > C:\TEMP\$($printer)_netsetuppg.html
$ie.Stop()
$ie.Quit()
I am successfully getting my scenario on below powershell script to auto login to the website xyz.com via IE but some functionality are not working properly, whereas firefox is doing same job perfectly. hence need help to convert the same script to perform same job in firefox browser using powershell.
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible
$username="username"
$password="password"
$Tenant="abcdefgh"
$ie.Navigate("htt:/xyz.com/login") ## website is taken as example
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
$usernamefield = $ie.document.getElementByID('txtudsr')
$usernamefield.value = "$username"
$passwordfield = $ie.document.getElementByID('txtpswd')
$passwordfield.value = "$password"
$Tenantfield = $ie.document.getElementByID('txttfenant')
$Tenantfield.value = "$Tenant"
$Link = $ie.document.getElementByID('btn_login')
$Link.click()
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 30;}
$Link = $ie.document.getElementByID('btn_Continue')
$Link.click()
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 10;}
$Link = $ie.document.getElementByID('statusUnAvailable')
$Link.click()
$ie.Quit()
I don't think it's a good idea do the same on firefox as you do for IE.
Your best friend will be selenium, it is the modern way to do browser automation nowadays.
I normally use selenium on Python, but there are some articles about selenium on PowerShell too. Just google it.
https://github.com/adamdriscoll/selenium-powershell
https://gist.github.com/Jaykul/d16a390e36ec3ba54cd5e3f760cfb59e
recently I began to learn PowerShell to automate my job tasks.
So I want to access a web page and click on a button that download automatically an Excel file. This is the button that I want to click on:
<div class="NormalButton">
<a class="ActiveLink" title="Excel" alt="Excel" onclick="$find('ctl32').exportReport('EXCELOPENXML');" href="javascript:void(0)" style="padding:3px 8px 3px 8px;display:block;white-space:nowrap;text-decoration:none;">Excel</a>
</div>
This would be my PowerShell script:
$ie = New-Object -com "InternetExplorer.Application"
$ie.Navigate("http://test.test/")
$ie.Visible = $true
$link = $ie.Document.getElementsByTagName('a') | Where-Object {$_.onclick -eq "$find('ctl32').exportReport('EXCELOPENXML');"}
$link.click()
If I try to run it from the console I receive the error "You cannot call a method on a null-valued expression."
If its useful I'm using PowerShell 4.0 and the webpage has a delay until the report is loaded.
I have completed it by the following code:
[void][System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
$url = "https://webpage.com"
$ie = New-Object -com internetexplorer.application
$ie.navigate($url)
$ie.StatusBar = $false
$ie.ToolBar = $false
$ie.visible = $true
#Get Excel
Start-Sleep -s 40
$btnExcel = $ie.Document.links | where-object { $_.outerText -eq 'Excel' -and $_.innerText -eq 'Excel' }
$btnExcel.click()
# Get Internet Explorer Focus
Start-Sleep -s 5
[Microsoft.VisualBasic.Interaction]::AppActivate("internet explorer")
[System.Windows.Forms.SendKeys]::SendWait("{F6}");
[System.Windows.Forms.SendKeys]::SendWait("{TAB}");
[System.Windows.Forms.SendKeys]::SendWait(" ");
Start-Sleep 1
[System.Windows.Forms.SendKeys]::SendWait("$file");
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}");
# Get Internet Explorer Focus
Start-Sleep -s 1
[Microsoft.VisualBasic.Interaction]::AppActivate("internet explorer")
[System.Windows.Forms.SendKeys]::SendWait("^{F4}");
Thank you all for you time :)
I Think the Your problem is because you are using Double Quotes which powershell threat it like variable and Try to expand it, so try to change it to Single quote, from:
$link = $ie.Document.getElementsByTagName('a') | Where-Object {$_.onclick -eq "$find('ctl32').exportReport('EXCELOPENXML');"}
to:
$link = $ie.Document.getElementsByTagName('a') | Where-Object {$_.onclick -eq '$find('ctl32').exportReport('EXCELOPENXML');'}
Also, you can change the -eq to -match and take just a portion of it like:
$_.onclick -match '$find('ctl32').exportReport'
For more information see: About Quoting Rules