Popup Yes/No message Box When Launching Powershell From a Batch File

Viewed 235

I'm struggling to find an answer to this, which might mean I'm asking the wrong questions.

I wrote a PS1 script that had popups and everything worked great! I used this method:

$msgBoxInput =  [System.Windows.MessageBox]::Show("You are about to publish Part $UserPart1, would you like to coninue?",'Confirm Update','YesNo')switch  ($msgBoxInput) 
      {
          'Yes' 
          {
          Runs Code
          }
          'No' 
          {
          Return
          }
      }

That worked perfectly. That is until I launched the PS1 using a batch file.

This is the code I'm using to run the batch file:

Powershell.exe -executionpolicy remotesigned -windowstyle hidden -File  "C:\Updater 2.0.ps1"

The batch file works, but the popups don't happen.

So I switched gears and tried using the popups like this:

 $msgBoxInput =  [System.Windows.MessageBox]::Show("You are about to publish Part $UserPart1, would you like to coninue?",'Confirm Update','YesNo')

      

Once again, the message box doesn't pop up. If I remove the "$msgBoxInput =" at the start of the message, the box pops up, but it doesn't matter what the users selects, the code just runs as if they pressed "Yes."

This may be the totally wrong approach, I honestly don't know. I've always used batch files for my user group (I've got 30+ users) because it's easier than trying to use the actual PS1. If there is a better/easier route, I'm all ears!

This is my first form using PS1 so I could be doing something just super wrong too.

Thank you all for your help.

1 Answers

System.Windows.MessageBox type isn't loaded automatically in PowerShell (except ISE). The following code snippet works:

if ( $null -eq ('System.Windows.MessageBox' -as [type]) ) {
    Add-Type -AssemblyName PresentationFramework
}
$UserPart1 = "XXX"
$msgBoxInput =  [System.Windows.MessageBox]::Show(
    "You are about to publish Part $UserPart1, would you like to coninue?",
    'Confirm Update',
    'YesNo')
switch  ($msgBoxInput) 
      {
          'Yes' 
          {
            # your code here
            Return "Runs Code"
          }
          'No' 
          {
            Return "Runs Nothing"
          }
      }

Output (omitted -windowstyle hidden to see returned value in current cmd window):

enter image description here

Powershell.exe -executionpolicy remotesigned -File D:\PShell\SO\72366658.ps1
Runs Code
Related