3

I want to create a confirmation box. I want to have a popup box that says that it appears that this is the factory key. Do you wish to continue? and if so proceed with the rest of the script if not, exit script.

The following is my script:

if (Test-Path $USB2\sources\install3.swm) { 
    $caption = "*** IT APPEARS THIS IS A FACTORY KEY ***"    
    $message = "Are you Sure You Want To Proceed:"
    [int]$defaultChoice = 1
    $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "I understand, flash over it."
    $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No"
    $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
    $choiceRTN = $host.UI.PromptForChoice($caption, $message, $options, $defaultChoice)

    if ( $choiceRTN -ne 1 ) {
        "Your Choice was Yes"
    } else {
        Stop-Process -Id $PID
    }
}

See the bit, $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no). Where I am stuck is the "Your Choice was yes" I don't need that... I just need it to continue with the script if yes. How do I do that?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Rj McKay
  • 53
  • 1
  • 6
  • 1
    have you looked at using the `-Confirm` Flag that is part of many Cmdlets? At any rate, I would see https://stackoverflow.com/questions/24649019/how-to-use-confirm-in-powershell/24649481 – Josh E Oct 01 '19 at 16:17
  • 1
    `if ($choiceRTN -eq 1) { exit }` after that just add the rest of your script. – Theo Oct 01 '19 at 18:25

2 Answers2

5

Consider using the System.Windows.MessageBox...

Add-Type -AssemblyName PresentationFramework
[System.Windows.MessageBox]::Show('It appears that this is the factory key. Do you wish to continue?', 'Confirmation', 'YesNo');

enter image description here

For your use case,

# You need this to use System.Windows.MessageBox
Add-Type -AssemblyName 'PresentationFramework'

# By default, you're blasting the USB drive
$continue = 'Yes'

# If you see the file, prompt the user
if ( $(Test-Path -Path "${USB2}\sources\install3.swm") ) { 
    $caption = "*** IT APPEARS THIS IS A FACTORY KEY ***"    
    $message = "Are you Sure You Want To Proceed:"

    $continue = [System.Windows.MessageBox]::Show($message, $caption, 'YesNo');
}

if ($continue -eq 'Yes') {
    # The user either (a) said "yes" to the "do you prompt" or
    # (b) the file didn't exist.

    Write-Output "Flash over..."
    # ...do the flashing here...
} else {
    # The user said "no" to the flash prompt

    Write-Output "Terminating process..."
    Start-Sleep 1
}

The message box will return Yes or No, and you can use that in your control flow to determine next steps. If that includes closing the window, call exit.

Adam
  • 3,891
  • 3
  • 19
  • 42
  • That looks fantastic! Exactly what I wanted to do. Question (I'm also going to try it myself...) for the if $continue -eq 'Yes' … do you put the rest of the entire script in the {} ? – Rj McKay Oct 01 '19 at 20:28
  • Yep, that’s sort of what I was thinking you’d do. – Adam Oct 01 '19 at 20:28
  • Mark as answer? – Rudy2015 Oct 02 '19 at 12:45
  • I'm really close to marking as answer. But putting the rest of the code in the {} doesn't work so far... still figuring out why. Thanks for asking Rudy2015. – Rj McKay Oct 02 '19 at 13:45
  • actually, I did mark it as answer. I think this is the answer... I think I just implemented it wrong. Thanks =) – Rj McKay Oct 02 '19 at 13:57
  • Update your question with the new prob. Maybe we can help you. – Adam Oct 02 '19 at 14:11
  • @Adam, I noticed you updated your answer... thank you! I updated my code by your new example. Update this one or start a new thread? the only way I can think to show my new prob is to post my entire script since the yes condition means do the entire rest of the script. – Rj McKay Oct 02 '19 at 15:37
  • Do that. And DM me the new link to the quest. – Adam Oct 02 '19 at 15:41
0

The below prompts for input, stores the clicked value in a variable, and returns it as a string. Checking to see the value of $var and proceeding from there should be simple enough.

$var = [System.Windows.MessageBox]::Show('It appears this is a factory key. Do you wish to continue?','DialogTitle','YesNoCancel','Warning')
echo("The value of the dialog box is $($var)")
HelpfulHound
  • 326
  • 2
  • 9