Powershell - 提示用户在xx秒后过期提示

时间:2014-09-08 16:55:20

标签: powershell timer prompt timed

我正在尝试显示用户操作的提示,如果不采取任何操作,则提示关闭并继续脚本。这是我的命令提示符对话框configuraiton


$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",""
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No",""
$cancel = New-Object System.Management.Automation.Host.ChoiceDescription "&Cancel",""
$choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no,$cancel)
$caption = "Caption message here"
$dialogmessage = ("Do you want to do something?:`n")

$timer = #set some value
#While the value is grater than zero keep the prompt open

while($timer -gt 0)
{
    $result = $Host.UI.PromptForChoice($caption,$dialogmessage,$choices,0)

    if($result -eq 0){Write-Host "Yes was selected"}

    if($result -eq 1){Write-Host "No was selected"}

    if($result -eq 2){Write-Host "Canceled by user.";exit}
 }

我已经在互联网上上下移动了,我已经看到过在C#中实现了这一点,但对于我的生活,我无法确定PowerShell的最佳方法。我将不胜感激任何帮助。

谢谢

编辑:谢谢诺亚,这是更新后的代码,它更加紧凑!

        $prompt = new-object -comobject wscript.shell 
        $answer = $prompt.popup("Do you want to do something?`n",5,"title",3)              
        if($answer -eq 6) {Write-Host "Yes was selected"}
        if($answer -eq 7) {Write-Host "No was selected"}
        if($answer -eq -1) {Write-Host "Timed out"}
        if($answer -eq 2) {Write-Host "Canceled by user."}   

1 个答案:

答案 0 :(得分:8)

在这里你......从我的一个旧剧本中挖掘出来

#Value  Description   
#0 Show OK button. 
#1 Show OK and Cancel buttons. 
#2 Show Abort, Retry, and Ignore buttons. 
#3 Show Yes, No, and Cancel buttons. 
#4 Show Yes and No buttons. 
#5 Show Retry and Cancel buttons. 
#http://msdn.microsoft.com/en-us/library/x83z1d9f(v=vs.84).aspx

$a = new-object -comobject wscript.shell 
$intAnswer = $a.popup("Question?",2,"Title",4) #first number is timeout, second is display.

#7 = no , 6 = yes, -1 = timeout