2017-09-22 138 views
3

我試圖創建一個彈出窗口,在腳本結束之前一直保持打開狀態。Powershell關閉彈出框

我有下面的代碼創建一個彈出框

$wshell = New-Object -ComObject Wscript.Shell 

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

$wshell.quit 

我想通$ wshell.quit會關閉該窗口,但事實並非如此。有沒有一種方法可以在腳本中關閉此對話框而無需用戶交互?

+0

我不認爲那樣的彈出式設計以編程方式關閉。一種解決方案是創建自己的表單並關閉它。基本上,一個自定義的消息框? –

回答

0

因爲在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 
0

你可以從電源外殼,打開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?

0

請參閱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 
+0

由於您已將nSecondsToWait設置爲1,因此您無需對此進行計數,因此彈出時間將持續1秒。順便說一句,在PowerShell中你的代碼看起來就像是:'$ wshell =新物體-ComObject Wscript.Shell $數= 1 $結果= 0 雖然($結果-eq 0){$ 結果= $ wshell如果($ count -eq 10){ (「完成操作」,1,「完成」,0)#'= 1'不確定你在這裏想要什麼 $ count + = 1 寫主機$計數 退出 } }'無論如何,他希望彈出窗口保持打開狀態**直到腳本結束**。 – E235

+0

@ E235,iaa你不明白,這樣你可以讓對話等待,只要你想要,並且當你想要的OP請求時關閉它,並且不,如果你使用0,腳本將永遠不會完成,除非你按下按鈕或結束任務 – peter

+0

我把它作爲VBS腳本運行,我明白你的意思。但是當我運行它時,它會將標題更改爲數字「2」,並在按下「確定」後將其更改爲「3」,如此等等直到10爲止。而且,在VBS中,您可以運行'wShell.Popup(.. )'然後它會轉到下一個命令,但是在運行'$ wshell.Popup(..)'時,它會保持卡住,直到你關閉窗口窗體,在這種情況下,你需要使用powershel的作業。 – E235