Powershell关闭弹出框

时间:2017-09-22 17:48:54

标签: powershell vbscript

我正在尝试创建一个在脚本完成之前保持打开状态的弹出窗口。

我有以下代码来创建一个弹出框

$wshell = New-Object -ComObject Wscript.Shell

$wshell.Popup("Operation Completed",0,"Done",0x1)

$wshell.quit

我认为$ wshell.quit会关闭窗口,但事实并非如此。有没有办法在没有用户交互的情况下从脚本中关闭此对话框?

3 个答案:

答案 0 :(得分:0)

您可以在电源shell中打开Internet Explorer,显示本地html页面(又名启动画面),然后在完成后关闭它

$IE=new-object -com internetexplorer.application
$IE.navigate2("www.microsoft.com")
$IE.visible=$true
 ...
$IE.Quit()

有些人在这里阅读https://social.technet.microsoft.com/Forums/ie/en-US/e54555bd-00bb-4ef9-9cb0-177644ba19e2/how-to-open-url-through-powershell?forum=winserverpowershell

在此处阅读更多内容How to properly close Internet Explorer when launched from PowerShell?

答案 1 :(得分:0)

有关参数,请参阅https://msdn.microsoft.com/en-us/library/x83z1d9f(v=vs.84).aspx。 你需要的是[nSecondsToWait],如果值为0,则脚本不等待,使用秒的值,它将自行关闭。

intButton = wShell.Popup(strText,[nSecondsToWait],[strTitle],[nType])

另一种方法是使用wshShell.SendKeys "%N"

向关键字发送密钥

在这里使用第一种方法,你可以做一些例子。 我在这里使用vbscript,没有使用PowerShell的经验,但它几乎是相同的解决方案。

Set wShell = WScript.CreateObject("WScript.Shell")
count  = 1
result = -1
Do while result = -1
  result = wShell.Popup("Operation Completed", 1, "Done", 0)
  count = count + 1
  Wscript.echo count
  if count = 10 then Exit Do ' or whatever condition
Loop

答案 2 :(得分:0)

$wsheel.quit的使用在这里不起作用,因为在PowerShell执行$wshell.Popup(..)时,会话将等待,直到表单关闭。
您无法运行任何其他命令,直到窗口关闭。

您可以做的是在不同的会话中创建弹出窗口,然后您可以运行代码,当代码完成时,搜索作业并将其删除。

解决方案#1:

function killJobAndItChilds($jobPid){
    $childs = Get-WmiObject win32_process | where {$_.ParentProcessId -eq $jobPid}
    foreach($child in $childs){
        kill $child.ProcessId
    }
}

function Kill-PopUp($parentPid){
    killJobAndItChilds $parentPid
    Get-Job | Stop-Job
    Get-Job | Remove-Job
}

function Execute-PopUp(){
    $popupTitle = "Done"
    $popupScriptBlock = {
       param([string]$title)
       $wshell = New-Object -ComObject Wscript.Shell

       $wshell.Popup("Operation Completed",0,$title,0x1)
    }

    $job = Start-Job -ScriptBlock $popupScriptBlock -ArgumentList $popupTitle

    # Waiting till the popup will be up.
    # Can cause bugs if you have other window with the same title, so beaware for the title to be unique
    Do{
        $windowsTitle = Get-Process | where {$_.mainWindowTitle -eq $popupTitle }
    }while($windowsTitle -eq $null)
}

Execute-PopUp

#[YOUR SCRIPT STARTS HERE]
Write-Host "Your code"
Start-Sleep 3
#[YOUR SCRIPT ENDs HERE]


Kill-PopUp $pid

它会创建您的弹出窗口,并且仅在窗口启动时(通过标题进行验证。请注意,如果有另一个具有相同窗口标题的进程,它可能会导致崩溃)您的代码将开始运行。<登记/> 当你的代码完成后,它将终止你的工作 请注意,我没有使用Stop-Job来停止工作 我猜是因为当作业创建弹出窗口时,它无法接收任何命令,直到弹出窗口关闭。
为了克服它,我终止了这个工作的过程。

解决方案#2(使用事件):

function Kill-PopUp(){
    kill (Get-Event -SourceIdentifier ChildPID).Messagedata
    Get-Job | Stop-Job
    Get-Job | Remove-Job
}

function Execute-PopUp(){
    $popupTitle = "Done"
    $popupScriptBlock = {
       param([string]$title)
       Register-EngineEvent -SourceIdentifier ChildPID -Forward
       New-Event -SourceIdentifier ChildPID -MessageData $pid > $null
       $wshell = New-Object -ComObject Wscript.Shell

       $wshell.Popup("Operation Completed",0,$title,0x1)
    }

    $job = Start-Job -ScriptBlock $popupScriptBlock -ArgumentList $popupTitle

    # Waiting till the popup will be up.
    # Can cause bugs if you have other window with the same title, so beaware for the title to be unique
    Do{
        $windowsTitle = Get-Process | where {$_.mainWindowTitle -eq $popupTitle }
    }while($windowsTitle -eq $null)
}

Execute-PopUp

#[YOUR SCRIPT STARTS HERE]
Write-Host "Your code"
Start-Sleep 3
#[YOUR SCRIPT ENDs HERE]


Kill-PopUp