5

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.

5
  • 1
    2nd code sample should work when you remove ;}" at the end of the first line. Commented May 24, 2022 at 16:58
  • 2
    I haven't tested at all, but I can't help but wonder if -windowstyle hidden is the problem. Does the window being hidden also hide pop-ups? Commented May 24, 2022 at 17:20
  • @zett42 that was a bad copy/paste on my part. That snipet isn't actually in there. I have corrected the post. Commented May 24, 2022 at 17:45
  • @TheMadTechnician I just tried removing the hidden bit and the popups still don't work :( Commented May 24, 2022 at 17:46
  • Where is defined $UserPart1? Commented May 24, 2022 at 19:15

1 Answer 1

8

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
Sign up to request clarification or add additional context in comments.

3 Comments

That works brilliantly! Thanks mate! Now I just have to figure out how to make the cmd window invisible again!
@Lowendz113 Put back -windowstyle hidden to the Powershell.exe … line.
Great thanks - after using your code and realising it solved my problem, I checked 'Add-Type' and found "beginning in PowerShell 7, Add-Type doesn't compile a type if a type with the same name already exists..." so you can now omit the $null check....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.