none
Powershell Yes/No Popup RRS feed

  • General discussion

  • I came across this very cute little script which offers the user a Yes/No dialog box:

    # Script name: YesNoPrompt.ps1
    # Created on: 2007-01-07
    # Author: Kent Finkle
    # Purpose: How Can I Give a User a Yes/No Prompt in Powershell?
     
    $a = new-object -comobject wscript.shell
    $intAnswer = $a.popup("Do you want to delete these files?", `
    0,"Delete Files",4)
    If ($intAnswer -eq 6) {
      $a.popup("You answered yes.")
    } else {
      $a.popup("You answered no.")
    }
     
    #Button Types 
    #
    #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.
    
    

    It beats Read-Host for this kind of input.


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    • Changed type Bigteddy Sunday, August 21, 2011 8:39 PM
    Sunday, August 21, 2011 8:38 PM

All replies

  • Do you mean this - Net Framework compatible method:

    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic")
    [Microsoft.VisualBasic.Interaction]::MsgBox("Do you agree?",'YesNoCancel,Question', "Respond please")

    The plus is you do not need to decode the return because it returns strings Yes/No/Cancel.

    [Microsoft.VisualBasic.Interaction]::InputBox("Do you agree?",'YesNoCancel,Question', "Respond please")


    [Microsoft.VisualBasic.Interaction]::<tab>

    [Microsoft.VisualBasic.Interaction]| gm -static

    get ini file sections

    [Microsoft.VisualBasic.Interaction]::GetAllSettings('Myapp','section1')


    jv
    Sunday, August 21, 2011 9:34 PM
  • I almost forgot one of teh most important of the VB support methods.

    [Microsoft.VisualBasic.Strings]::StrConv("hello from the inside of vb",'ProperCase')

     


    jv
    Sunday, August 21, 2011 9:46 PM
  • Thanks, jv, I had no idea there was a 'VisualBasic' .NET class.  VERY cool.

    Although it's a pity that Powershell is so limited when it comes to graphical user input/output.


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Monday, August 22, 2011 6:45 AM
  • Although it's a pity that Powershell is so limited when it comes to graphical user input/output.

    "Surely You Must Be Joking, Mr. Teddy"

    Where is this limit?  We can use Windows Forms, XAML, HTA, IE.  What is missing?

     

     


    jv
    Monday, August 22, 2011 7:01 AM
  • Yes, I realise the huge power and potential of Powershell, with it's access to the .NET framework.

    I am referring to built-in, native Powershell graphical user I/O, such as Out-GridView, and Read-Host.  It would be nice to have a cmdlet like Show-MessageBox for example.  I'm just dreaming.

    And I can hear what you are going to say next:  Build it yourself with a function.


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Monday, August 22, 2011 7:35 AM
  • "Surely You Must Be Joking, Mr. Teddy"

     jv

    Did you use the

    [Microsoft.VisualBasic.Strings]::StrConv("surely you must be joking, mr. teddy",'ProperCase')

    ...to construct this sentence?  It's not like you to have such a 'Proper' keyboard :-)


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Monday, August 22, 2011 7:38 AM
  • .

    I am referring to built-in, native Powershell graphical user I/O, such as Out-GridView, and Read-Host.  It would be nice to have a cmdlet like Show-MessageBox for example.  I'm just dreaming.

    PowerShell was purposely built without a GUI because it was intended primarily for administration.  GUIs are a useful extension but were left out to reduce the complexity of teh shell.

    All shells lack fundamental GUI capability.  There are a few extended shells that have native support for a GUI. Consider VBScript.  It is either popup or Commandline.  If you want other you need to be explicit.(msgbox /popup)

    A message box function can be easily added but has probably been considered as unnecessary in the foundation product.

    Due to the cry for an editor we have the ISE.  It works but, in my opionion, is a hack at a solution that the PoSH team will forever regret.  It is a distraction from the fundamental product.  It is a good product for third party developers which have now created a dozen better editor/debugger programs.

     


    jv
    Monday, August 22, 2011 8:10 AM
  •  It's not like you to have such a 'Proper' keyboard :-)
    It is the title of a book....
    jv
    Monday, August 22, 2011 8:10 AM
  • Due to the cry for an editor we have the ISE.  It works but, in my opionion, is a hack at a solution that the PoSH team will forever regret.  It is a distraction from the fundamental product.  It is a good product for third party developers which have now created a dozen better editor/debugger programs.

     


    jv

    As a free tool, I think the ISE isn't bad.  But compared to other free tools, like Visual Studio Express, it's terrible. 

    I use the ISE all the time, but I wish for something with better debugging capabilities.  Any suggestions?

    PS: That book looks interesting, I'll give it a read.


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Monday, August 22, 2011 8:14 AM
  • Put this in your profile.

    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    $xlr8r = [type]::gettype("System.Management.Automation.TypeAccelerators")
    $xlr8r::Add('msgbox',[System.Windows.Forms.MessageBox])
    [msgbox]::show("hello",'Its a Happy Day','YesNoCancel')

    If you want a very good debugger look into Sapien PromalScript. I have been using it for years for all scripting languages.  It is extremely good and very mature.

    http://www.sapien.com/software/primalscript

     


    jv
    Monday, August 22, 2011 8:31 AM
  • Implemented as a function:

     

    # By BigTeddy August 22, 2011
    # Popup Types 
    # 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.
    function Show-MessageBox
      {
       [CmdletBinding()]
       Param(
        [Parameter(Mandatory=$true)]
        [string] $prompt,
        [Parameter(Mandatory=$false)]
        [string] $title,
        [Parameter(Mandatory=$false)]
        [int] $popuptype
       )
       Process
       {
       if ($title -eq "") {$title = $prompt}
       if ($popuptype -eq "") {$popuptype = 0}
       $a = new-object -comobject wscript.shell
       $intAnswer = $a.popup($prompt, 0,$title,$popuptype)
       $a = $null
       switch ($intAnswer) 
       { 
        1 {Return "OK"} 
        2 {Return "Cancel"} 
        3 {Return "Abort"} 
        4 {Return "Retry"} 
        5 {Return "Ignore"} 
        6 {Return "Yes"} 
        7 {Return "No"}
        default {}
       }
       }
      }
    
    

     


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Monday, August 22, 2011 6:59 PM
  • JV,

    Is sapein Primal forms any good if you want to build a GUI to run powershell commands?

     

    Tuesday, August 23, 2011 9:20 AM
  • JV,

    Is sapein Primal forms any good if you want to build a GUI to run powershell commands?

     


    It's fantastic for that reason! It even has a built in export to powershell button.
    Dan
    Tuesday, August 23, 2011 12:01 PM
  • Ok thank you, I’m looking at getting a copy of primal forms or maybe the bundle. I’m working on a script that will audit servers for various settings both hardware and software and JRV has helped me with a few commands J What I’d like to do is build a GUI / App that will allow you to enter a server name or select from a list / AD and then select what you’d like to run the check against. So I’ll need multiple tabs for different categories say hardware, software, networking…etc with radio buttons in each to be able to make the selections that will be present under each category. Then I’ll like the means to output this in csv, html or email etc. I would also like a section where you can select items to make corrections or changes to a server or servers. So I’m sure for some it is straight forward but for me it will be quite challenging however I’m hoping that I can use some of the commands in the script and then build on it. My only reservation is that Primal Forms don’t seem to issue a guide on how to do such things and for a newbie that’s not good. I know they have a forum and blogs but a guide or GUI templates /builder would be a good place to start for someone like me.

     

    Tuesday, August 23, 2011 2:13 PM
  • This is my Show-MsgBox function:

    Did you know, Teddy, that you can use selection lists as parameters in Advanced Functions.  This allows you to define a list of valid strings as a comma seperated list and the argument will be validated against that list.  This is to prevent you from having to do all of those bulky switch statements.


    jv
    Tuesday, August 23, 2011 10:07 PM
  • Nope, I didn't.  That's why I used those bulky switch statements!  Pray, tell more.
    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Tuesday, August 23, 2011 10:16 PM
  • Nope, I didn't.  That's why I used those bulky switch statements!  Pray, tell more.
    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"

    ValidateSet Attribute

        The ValidateSet attribute secifies a set of valid values for the
        argument of the parameter. The Windows PowerShell runtime generates
        an error if the parameter argument does not match a value in the set.
        In the following example, the argument of the parameter can contain
        only the names Steve, Mary, and Carl.

        Param
          (
            [parameter(Mandatory=$true)]
            [String[]]
            [ValidateRange("Steve", "Mary", "Carl")]
            $UserName
          )


    jv
    Tuesday, August 23, 2011 10:27 PM
  • New improved version:

    <#
          .SYNOPSIS 
          Shows a graphical message box, with various prompt types available.
    
          .DESCRIPTION
          Emulates the Visual Basic MsgBox function. It takes four parameters, of which only the prompt is mandatory
    
          .INPUTS
          The parameters are:-
          
          Prompt (mandatory): 
            Text string that you wish to display
            
          Title (optional):
            The title that appears on the message box
            
          Icon (optional). Available options are:
            Information, Question, Critical, Exclamation (not case sensitive)
            
          BoxType (optional). Available options are:
            OKOnly, OkCancel, AbortRetryIgnore, YesNoCancel, YesNo, RetryCancel (not case sensitive)
            
          DefaultButton (optional). Available options are:
            1, 2, 3
    
          .OUTPUTS
          Microsoft.VisualBasic.MsgBoxResult
    
          .EXAMPLE
          C:\PS> Show-MsgBox Hello
          Shows a popup message with the text "Hello", and the default box, icon and defaultbutton settings.
    
          .EXAMPLE
          C:\PS> Show-MsgBox -Prompt "This is the prompt" -Title "This Is The Title" -Icon Critical -BoxType YesNo -DefaultButton 2
          Shows a popup with the parameter as supplied.
    
          .LINK
          http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.msgboxresult.aspx
    
          .LINK
          http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.msgboxstyle.aspx
          #>
    # By BigTeddy August 22, 2011
    # http://social.technet.microsoft.com/profile/bigteddy/.
    
    function Show-MsgBox
    {
    
     [CmdletBinding()]
      param(
      [Parameter(Position=0, Mandatory=$true)] [string]$Prompt,
      [Parameter(Position=1, Mandatory=$false)] [string]$Title ="",
      [Parameter(Position=2, Mandatory=$false)] [ValidateSet("Information", "Question", "Critical", "Exclamation")] [string]$Icon ="Information",
      [Parameter(Position=3, Mandatory=$false)] [ValidateSet("OKOnly", "OKCancel", "AbortRetryIgnore", "YesNoCancel", "YesNo", "RetryCancel")] [string]$BoxType ="OkOnly",
      [Parameter(Position=4, Mandatory=$false)] [ValidateSet(1,2,3)] [int]$DefaultButton = 1
      )
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic")
    switch ($Icon) {
          "Question" {$vb_icon = [microsoft.visualbasic.msgboxstyle]::Question }
          "Critical" {$vb_icon = [microsoft.visualbasic.msgboxstyle]::Critical}
          "Exclamation" {$vb_icon = [microsoft.visualbasic.msgboxstyle]::Exclamation}
          "Information" {$vb_icon = [microsoft.visualbasic.msgboxstyle]::Information}}
    switch ($BoxType) {
          "OKOnly" {$vb_box = [microsoft.visualbasic.msgboxstyle]::OKOnly}
          "OKCancel" {$vb_box = [microsoft.visualbasic.msgboxstyle]::OkCancel}
          "AbortRetryIgnore" {$vb_box = [microsoft.visualbasic.msgboxstyle]::AbortRetryIgnore}
          "YesNoCancel" {$vb_box = [microsoft.visualbasic.msgboxstyle]::YesNoCancel}
          "YesNo" {$vb_box = [microsoft.visualbasic.msgboxstyle]::YesNo}
          "RetryCancel" {$vb_box = [microsoft.visualbasic.msgboxstyle]::RetryCancel}}
    switch ($Defaultbutton) {
          1 {$vb_defaultbutton = [microsoft.visualbasic.msgboxstyle]::DefaultButton1}
          2 {$vb_defaultbutton = [microsoft.visualbasic.msgboxstyle]::DefaultButton2}
          3 {$vb_defaultbutton = [microsoft.visualbasic.msgboxstyle]::DefaultButton3}}
    $popuptype = $vb_icon -bor $vb_box -bor $vb_defaultbutton
    $ans = [Microsoft.VisualBasic.Interaction]::MsgBox($prompt,$popuptype,$title)
    return $ans
    } #end function
    


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Tuesday, August 23, 2011 10:40 PM
  • I still have to use the switch statements to assign the correct msgboxstyle value to a variable that can be -bor 'ed.  I don't see a more elegant way of doing it.
    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Tuesday, August 23, 2011 10:42 PM
  • I still have to use the switch statements to assign the correct msgboxstyle value to a variable that can be -bor 'ed.  I don't see a more elegant way of doing it.
    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"


    Same thing Just use many of any in the list but only one of each then jsut add them together.

    This is also why using the VisualBasic class or the System.Windows,Forms,MessageBox are both easier. They can just take the strings directly and repeatedly. Net classes are like that.  The COM code is not managed code and will not be useable in some scenarios, whereas, the NET classes are CLR and are managed code.

    I understand that the classic VBScript msgbox is familiar but it is no different from the other two and harder to manage. 

    Anyway look  into the different kinds of valicatd sets that are available in CmdLets. You might find them useful.

    Your CmdLet look pretty good.

    help about_functions_advanced_param

     

     


    jv
    Tuesday, August 23, 2011 10:50 PM