I'm using IE11's COM object in Powershell to authenticate to a website and pull down a list of orders. So far, my script is successfully sending the username and password as well as getting to the orders page and I can even "click" on the link that downloads the file I need. The problem is that instead of saving the file like I need to do the open/saveas dialog comes up and the script bombs.
I've attempted to have IE11 automatically save the file by editing the registry and specifying the Excel.CSV type but this didn't work. The download link is actually a button that calls a complicated Javascript function and in the end results of the CSV file I need but I've tried to decipher the JS that issues a POST request and use the Navigate() method on the IE COM object to no avail.
I realize this isn't the best way to automate a web process but I couldn't figure out how to work around all the Javascript on the page to use a cmdlet like Invoke-WebRequest. It's kinda hard to just use HTTP GETs and POSTS when EVERYTHING is Javascript!
Any help is greatly appreciated. Here's a code sample.
Function Get-PageElement ($Name,$TagName,$Id) {
if ($Name) {
$InternetExplorer.Document.getElementsByName($Name)
} elseif ($TagName) {
$InternetExplorer.Document.getElementsByTagName($TagName)
} elseif ($Id) {
$InternetExplorer.Document.getElementById($Id)
}
}
$script:InternetExplorer = New-Object -com "InternetExplorer.Application"
$script:InternetExplorer.visible = $true
$InternetExplorer.Navigate($Url)
$DownloadButton = Get-PageElement -Id btnExport
$DownloadButton.Click()
If you can't parse the javascript and get the url you need you can use wscript to send the key strokes. Note that this is probably a terrible idea and I wouldn't recommend it if there is any way at all to parse the javascript and get the raw url to use a WebClient to download.