fbpx Program: Add a Graphical User Interface to Your Script | Windows PowerShell, Windows Server | HostingUltraso.com
24/7/365 Support

Program: Add a Graphical User Interface to Your Script

While the techniques provided in the rest of this chapter are usually all you need, it is sometimes helpful to provide a graphical user interface to interact with the user.

Since PowerShell fully supports traditional executables, simple programs can usually fill this need. If creating a simple program in an environment such as Visual Studio is inconvenient, you can often use PowerShell to create these applications directly.

Example 129 demonstrates the techniques you can use to develop a Windows Forms application using PowerShell scripting alone.

Example 129. SelectGraphicalFilteredObject.ps1

############################################################################## ## ## SelectGraphicalFilteredObject.ps1 ## ## Display a Windows Form to help the user select a list of items piped in. ## Any selected items get passed along the pipeline. ## ## ie: ## ## PS >dir | SelectGraphicalFilteredObject ##

Example 129. SelectGraphicalFilteredObject.ps1 (continued)

## Directory: Microsoft.PowerShell.Core\FileSystem::C:\

##

##

## Mode
LastWriteTime Length Name

##

## d
10/7/2006
4:30 PM
Documents and Settings

## d
3/18/2007
7:56 PM
Windows

##

 

##############################################################################

$objectArray = @($input)

## Ensure that they've piped information into the script if($objectArray.Count eq 0) {

WriteError "This script requires pipeline input." return }

## Load the Windows Forms assembly [void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

## Create the main form $form = NewObject Windows.Forms.Form $form.Size = NewObject Drawing.Size @(600,600)

## Create the listbox to hold the items from the pipeline $listbox = NewObject Windows.Forms.CheckedListBox $listbox.CheckOnClick = $true $listbox.Dock = "Fill" $form.Text = "Select the list of objects you wish to pass down the pipeline" $listBox.Items.AddRange($objectArray)

## Create the button panel to hold the OK and Cancel buttons $buttonPanel = NewObject Windows.Forms.Panel $buttonPanel.Size = NewObject Drawing.Size @(600,30) $buttonPanel.Dock = "Bottom"

## Create the Cancel button, which will anchor to the bottom right $cancelButton = NewObject Windows.Forms.Button $cancelButton.Text = "Cancel" $cancelButton.DialogResult = "Cancel" $cancelButton.Top = $buttonPanel.Height $cancelButton.Height 5 $cancelButton.Left = $buttonPanel.Width $cancelButton.Width 10 $cancelButton.Anchor = "Right"

## Create the OK button, which will anchor to the left of Cancel $okButton = NewObject Windows.Forms.Button $okButton.Text = "Ok" $okButton.DialogResult = "Ok" $okButton.Top = $cancelButton.Top $okButton.Left = $cancelButton.Left $okButton.Width 5 $okButton.Anchor = "Right"

Example 129. SelectGraphicalFilteredObject.ps1 (continued)

## Add the buttons to the button panel $buttonPanel.Controls.Add($okButton) $buttonPanel.Controls.Add($cancelButton)

## Add the button panel and list box to the form, and also set ## the actions for the buttons $form.Controls.Add($listBox) $form.Controls.Add($buttonPanel) $form.AcceptButton = $okButton $form.CancelButton = $cancelButton $form.Add_Shown( { $form.Activate() } )

## Show the form, and wait for the response $result = $form.ShowDialog()

## If they pressed OK (or Enter,) go through all the ## checked items and send the corresponding object down the pipeline if($result eq "OK") {

foreach($index in $listBox.CheckedIndices) { $objectArray[$index] } }

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say