I am trying to display a promp for user action and if no action is taken have the prompt close and teh script continue on. here is my command prompt dialog 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}
}
I have looked up and down the internet I have seen this implemented in C# but for the life of me i cannot determine the best approach for powershell. I would appreciate any help.
Thank you
Edit: Thank you Noah, here is the updated code, it is even more compact too!
$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."}