PowerShell: Graphic User Interface


This is just for fun – I built a Toolbox for myself which includes some useful PowerShell scripts I wrote before. It looks simple but very handy, just a simple click on the script name and it will run.

image

An example, I am checking the hosts info in the DHCP server.

image

# my_toolbox.ps1 source codes

[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)

# Add a new form
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = “My Toolbox”
$objForm.Size = New-Object System.Drawing.Size(300,250)
$objForm.StartPosition = “CenterScreen”

# Add a new close button
$CloseButton = New-Object System.Windows.Forms.Button
$CloseButton.Location = New-Object System.Drawing.Size(110,180)
$CloseButton.Size = New-Object System.Drawing.Size(75,23)
$CloseButton.Text = “Close”
$CloseButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($CloseButton)

# Add a new label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,15)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = “Please single click the tool to run:”
$objForm.Controls.Add($objLabel)

# Add a new listbox
$objCmdletsListbox = New-Object System.Windows.Forms.Listbox
$objCmdletsListbox.Location = New-Object System.Drawing.Size(30,40)
$objCmdletsListbox.Size = New-Object System.Drawing.Size(220,40)

# Add all scripts’ name in C:\Scripts\mytools.txt, and copy all scripts and associated files into C:\Scripts\
$items = Get-Content “C:\Scripts\mytools.txt”

# Add scripts into the listbox
foreach ($item in $items)
{
    [void] $objCmdletsListbox.Items.Add($item)
}

$objCmdletsListbox.Height = 120
$objForm.Controls.Add($objCmdletsListbox)

# Defind the single click action
$objCmdletsListbox.add_Click(
  {
    $tool = $objCmdletsListbox.SelectedItem
    Write-Host $tool
    $result = Invoke-Expression C:\Scripts\$tool
  }
)

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

$x

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s