Powershell - request user, then expire in xx seconds

I am trying to display promp for a user action, and if no action has been taken, the prompt to close and the script continue. here is my configuraiton command line dialog


$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} } 

I looked up and down the internet. I saw how this is implemented in C #, but for life I can not determine the best approach for powershell. I would appreciate any help.

thanks

Edit: Thanks, Noah, here is the updated code, it's even more compact!

  $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.";exit} 
+6
source share
1 answer

Here you go ... dug this from one of my old scripts

 #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 
+6
source

Source: https://habr.com/ru/post/974990/


All Articles