我试图创建一个弹出窗口,在脚本结束之前一直保持打开状态。Powershell关闭弹出框
我有下面的代码创建一个弹出框
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Operation Completed",0,"Done",0x1)
$wshell.quit
我想通$ wshell.quit会关闭该窗口,但事实并非如此。有没有一种方法可以在脚本中关闭此对话框而无需用户交互?
我试图创建一个弹出窗口,在脚本结束之前一直保持打开状态。Powershell关闭弹出框
我有下面的代码创建一个弹出框
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Operation Completed",0,"Done",0x1)
$wshell.quit
我想通$ wshell.quit会关闭该窗口,但事实并非如此。有没有一种方法可以在脚本中关闭此对话框而无需用户交互?
因为在PowerShell中,当你执行$wshell.Popup(..)
采用$wsheel.quit
不会在这里工作会议将等待,直到关闭窗体。
您将无法运行任何其他命令,直到窗口将被关闭。
你可以做的是在不同的会话中创建弹出窗口,通过它你可以运行你的代码,当你的代码完成后,搜索工作并杀死它。
解决方案#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
它创建弹出窗口,只有当窗口了(由标题验证请注意,它可能会导致colissions如果用相同的另一种方法。窗口的标题)你的代码将开始运行。
当您的代码完成时,它将会终止该作业。
请注意,我没有使用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
你可以从电源外壳,打开Internet Explorer,显示本地HTML网页(又称闪屏),那么内,完成后,关闭它
$IE=new-object -com internetexplorer.application
$IE.navigate2("www.microsoft.com")
$IE.visible=$true
...
$IE.Quit()
一些更多阅读请点击这里How to properly close Internet Explorer when launched from PowerShell?
请参阅https://msdn.microsoft.com/en-us/library/x83z1d9f(v=vs.84).aspx的参数。 你需要的是[nSecondsToWait],如果该值为0,脚本会不自然地等待,使用一个值作为秒数,并且它本身会自动关闭。
intButton = wShell.Popup(strText中,[nSecondsToWait],[strTitle],[n类型])
另一种方式是使用第一方法来发送keystoke到对话框与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
由于您已将nSecondsToWait设置为1,因此您无需对此进行计数,因此弹出时间将持续1秒。顺便说一句,在PowerShell中你的代码看起来就像是:'$ wshell =新物体-ComObject Wscript.Shell $数= 1 $结果= 0 虽然($结果-eq 0){$ 结果= $ wshell如果($ count -eq 10){ (“完成操作”,1,“完成”,0)#'= 1'不确定你在这里想要什么 $ count + = 1 写主机$计数 退出 } }'无论如何,他希望弹出窗口保持打开状态**直到脚本结束**。 – E235
@ E235,iaa你不明白,这样你可以让对话等待,只要你想要,并且当你想要的OP请求时关闭它,并且不,如果你使用0,脚本将永远不会完成,除非你按下按钮或结束任务 – peter
我把它作为VBS脚本运行,我明白你的意思。但是当我运行它时,它会将标题更改为数字“2”,并在按下“确定”后将其更改为“3”,如此等等直到10为止。而且,在VBS中,您可以运行'wShell.Popup(.. )'然后它会转到下一个命令,但是在运行'$ wshell.Popup(..)'时,它会保持卡住,直到你关闭窗口窗体,在这种情况下,你需要使用powershel的作业。 – E235
我不认为那样的弹出式设计以编程方式关闭。一种解决方案是创建自己的表单并关闭它。基本上,一个自定义的消息框? –