简体   繁体   English

用PowerShell关闭弹出窗口

[英]Closing a popup window with powershell

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. 目前我正在编写一个脚本来自动化IE中的进程以添加计算机名称及其MAC,以便我们可以对它们进行成像。 The page has two fields one for MAC and one for a computer name then a add new button. 该页面有两个字段,一个用于MAC,一个用于计算机名称,然后是添加新按钮。 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. 目前它仍然是手动输入,但我打算从csv读取一次做大组。

The problem I am currently having is whenever there is a duplicate record it tells you with a javascript popup. 我目前遇到的问题是,只要有重复的记录,它就会通过javascript弹出窗口告诉您。 The popup disables interaction with the IE page. 弹出窗口禁用与IE页面的交互。 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. 我暂时决定使用一个草率的解决方案来关闭每个新条目的com对象。 I am however now having issues with importing a csv for the script to read. 但是,我现在遇到导入csv以便脚本读取的问题。

$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. 此代码将活动窗口设置为标题为"Message from webpage"的窗口,因此调整它以匹配弹出标题。

Then it will send the Enter key press. 然后它将按Enter键发送。 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}') 甚至可能使用::SendWait('%{f4}')发送ALT+F4

I am assuming the Javascript pop-up opens in a new window. 我假设Javascript弹出窗口在新窗口中打开。 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. 或者,我会看到页面上使用了其他JavaScript,如果只是弹出窗口,则在该页面上为com对象禁用Javascript。

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. 要禁用Javascript,您需要关闭此页面的所有Java代码执行,方法是将其添加到受限制的站点安全区域,并确保关闭该区域的javascript执行。 But I have a better idea, you could use the com object to try and break the pages pop up function: 但我有一个更好的主意,你可以使用com对象尝试打破页面弹出功能:

$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. 此代码块打开一个具有演示javascript弹出功能的页面,然后从按钮onclick事件中删除函数调用。 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. 你可以做我修改页面的客户端副本并打破javascript或修改它永远不会显示的相同技巧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM