Powershell and Forms (part 1)

Using Powershell to create forms

I know this goes against the rules of scripting but I find this part of Powershell very useful if you are creating scripts for users that don’t know much about Powershell.  It also gives us the ability to restrict users in the commands they can run (and potentially save you from their errors).  So here is a little script to test and explain how to use Powershell to create forms.  There are applications out there that allow you to do all this and package it to create applications.

#   Create the contents of your dropdown list.  This can be a static list as below
#   [array]$MyDropDownList = "1. My first selection","2. My second selection","Exit"
#   or it can be generated from a powershell command.

# This line has been updated to remove displaying restrictions 
$MyDropDownList =   (Get-Service | Sort-Object displayname  ).displayname

#   This function will return the exact text from the selected choice
function Return-MySelection {
 $script:Choice = $Combo.SelectedItem.ToString()
 $Form.Close()
}

#   Function that allows you to select from the combobox in your form
function SelectOption{
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

    # Create a new Form Object
    $Form = New-Object System.Windows.Forms.Form

    # Create the size of your form 
    $Form.width = 500
    $Form.height = 200

    # Set the name of the form 
    $Form.Text = ”This is my First Form”

    # Set the font of the text to be used within the form
    $Font = New-Object System.Drawing.Font("Times New Roman",12)
    $Form.Font = $Font

    # Create a new ComboBox to be used in the form (
    $Combo = new-object System.Windows.Forms.ComboBox
    $Combo.Location = new-object System.Drawing.Size(130,20)
    $Combo.Size = new-object System.Drawing.Size(280,40)

    ForEach ($Item in $MyDropDownList) {
     [void] $Combo.Items.Add($Item)
    }

    $Form.Controls.Add($Combo)

    $LabelBox = new-object System.Windows.Forms.Label
    $LabelBox.Location = new-object System.Drawing.Size(10,20) 
    $LabelBox.size = new-object System.Drawing.Size(100,80) 
    $LabelBox.Text = "Select the service from the dropdown list"
    $Form.Controls.Add($LabelBox)

    # Inserting a Select button
    $SelectButton = new-object System.Windows.Forms.Button
    $SelectButton.Location = new-object System.Drawing.Size(130,50)
    $SelectButton.Size = new-object System.Drawing.Size(100,30)
    $SelectButton.Text = "Select"
    $SelectButton.Add_Click({Return-MySelection})
    $form.Controls.Add($SelectButton)

    # Inserting a Cancel button
    $CancelButton = new-object System.Windows.Forms.Button
    $CancelButton.Location = new-object System.Drawing.Size(235,50)
    $CancelButton.Size = new-object System.Drawing.Size(100,30)
    $CancelButton.Text = "Cancel"
    $CancelButton.Add_Click({$Form.Close()})
    $form.Controls.Add($CancelButton)

    $Form.Add_Shown({$Form.Activate()})
    [void] $Form.ShowDialog()
    return $script:choice
}

# call the function 
$option = selectOption

# Return your selection in text format
Write-Host $option

Have fun playing with the script and I will post more later this month.

6 comments

  1. Very nice post. I just stumbled uρon уour weblοg and wanted
    to say that I’ve reallʏ enjoyed browsing your blog posts.

    After all I will be subscribing to your rss feed and I hope you write
    again soon!

  2. Following on from Larry’s comment – changing line 6 to :

    $MyDropDownList = (Get-Service |Sort-Object displayname | select displayname).displayname

    will list all the services on a system in alphabetical order

  3. Adding the .displayname to line 6 gives a null result (nothing to select in dropdown). Removing .displayname provides dropdown elements of the form “@{DisplayName=Internet Explorer EKW Collector Service}”.

    Note in my case, I had to change “i*” to “I*”.
    — however that didn’t take away anything from the nice form example.

    1. Hi,Great ! I have the same problem with svrciee request in SCSM. I use PowerShell for update my svrciee request, then I update my review activities, then I update my runbook automation activies and my runbook Orchestrator do not start !Any help on how resolve this probleme without powershell ? Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.