At the moment I am working on a script to automate a process in IE to add Computer Names and their MACs so we can image them. The page has two fields one for MAC and one for a computer name then a add new button. At the moment it is still manual entry, but I intend for it to read from a csv to do large groups at a time.
The problem I am currently having is whenever there is a duplicate record it tells you with a javascript popup. The popup disables interaction with the IE page. I need a way to close the popups in between entries.
This is what my code currently looks like.
cls
$URL = ""
$iterator = 1;
#starts IE
$ie = new-object -ComObject "InternetExplorer.Application"
#visible for testing purposes
$ie.visible = $true
#$ie.silent = $true
$ie.navigate($URL)
#Manual Input
$newMAC = read-host -prompt 'Input MAC here'
$newNAME = read-host -prompt 'Input Computer Name here'
while($ie.Busy -eq $true) { start-sleep -Milliseconds 100 }
#enters information into appropriate fields and clicks the add button
($ie.document.getElementsByName("mc_id") |select -first 1).value = $newNAME;
($ie.document.getElementsByName("mac_id") |select -first 1).value = $newMAC;
($ie.document.getElementsByName("btnAddNew") |select -first 1).click();
Edit:
I have decided for the moment with a sloppy solution in closing my com object for each new entry. I am however now having issues with importing a csv for the script to read.
$wshell = New-Object -ComObject wscript.shell;
if($wshell.AppActivate('Message from webpage'))
{
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait('{Enter}')
}
This code will set the active window to the window that has the title of "Message from webpage" So adjust it to match the pop up title.
Then it will send the Enter key press. You might be able to adjust this to send a combination of keys to dismiss the pop up. Perhaps even send ALT+F4 using ::SendWait('%{f4}')
I am assuming the Javascript pop-up opens in a new window. Alternatively I would see what other JavaScript is used on the page and if its only for the pop-up look in to disabling Javascript on that page for the com object.
EDIT:
To disable the Javascript you would need to turn off all java code execution for this page by adding it to the restricted sites security zone and making sure the javascript execution for the zone is turned off. But I have a better idea, you could use the com object to try and break the pages pop up function:
$ie = new-object -ComObject "InternetExplorer.Application"
$ie.visible = $true
$ie.navigate("http://www.echoecho.com/jswindows03.htm")
$ie.Document.body.innerHTML = $ie.Document.body.innerHTML -replace "popup\('www.yahoo.com', 'Win1', 300, 300\);", ""
This code block opens a page that has a demo javascript pop up function, It then removes the function call from the buttons onclick event. Causing the pop to never be called. You can do the same trick I have done to modify the client copy of the page and break the javascript or modify it to never display.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With