Get your feet wet with Powershell GUI

Here’s my latest attempt in creating a simple GUI using Powershell for Windows Resource Management Tasks. Powershell by itself is easy but there is hardly any documentation on the web for creating a cool GUI. You have to put together various bits and pieces by the trial and error method. Anyway I did come up with some success. However there is a lot to be desired. For some reason the results only show up when the GUI is closed through the Cancel or Exit button. Feel free to tweak the code as you please. The Windows Management Commands are some basic scripts taken from Powershell Pro.

This is the GUI I created. Do read my post Powershell GUI – Adding bells & whistles for creating a full-fledged Powershell GUI

Feel free to send me any comments.

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

# Add functions
#*=============================================================================
Function BIOSInfo {
$colItems = Get-WmiObject Win32_BIOS -Namespace “root\CIMV2″ -computername $strComputer
foreach($objItem in $colItems) {
“—————————— Bios Info ———————”
“Computer Name: “+ $strComputer
“BIOS Characteristics: “+ $objItem.BiosCharacteristics
“BIOS Version: “+ $objItem.BIOSVersion
“Build Number: “+ $objItem.BuildNumber
“Caption: “+ $objItem.Caption
“Code Set: “+ $objItem.CodeSet
“Current Language: “+ $objItem.CurrentLanguage
“Description: “+ $objItem.Description
“Identification Code: “+ $objItem.IdentificationCode
“Installable Languages: “+ $objItem.InstallableLanguages
“Installation Date: “+ $objItem.InstallDate
“Language Edition: “+ $objItem.LanguageEdition
“List Of Languages: “+ $objItem.ListOfLanguages
“Manufacturer: “+ $objItem.Manufacturer
“Name: ” + $objItem.Name
“Other Target Operating System: “+ $objItem.OtherTargetOS
“Primary BIOS: “+ $objItem.PrimaryBIOS
“Release Date: “+ $objItem.ReleaseDate
“Serial Number: “+ $objItem.SerialNumber
“SMBIOS BIOS Version: “+ $objItem.SMBIOSBIOSVersion
“SMBIOS Major Version: “+ $objItem.SMBIOSMajorVersion
“SMBIOS Minor Version: “+ $objItem.SMBIOSMinorVersion
“SMBIOS Present: “+ $objItem.SMBIOSPresent
“Software Element ID: “+ $objItem.SoftwareElementID
“Software Element State: “+ $objItem.SoftwareElementState
“Status: “+ $objItem.Status
“Target Operating System: “+ $objItem.TargetOperatingSystem
“Version: “+ $objItem.Version

}
}

Function OSInfo {
$colItems = Get-WmiObject Win32_OperatingSystem -Namespace “root\CIMV2” -Computername $strComputer
foreach($objItem in $colItems) {
“—————————— OS Info ———————”
“Computer Name: “+ $strComputer
“Operating System:” + $objItem.Name
}
}

Function CPUInfo {
$colItems = Get-WmiObject Win32_Processor -Namespace “root\CIMV2” -Computername $strComputer
foreach($objItem in $colItems) {
“————————CPU Info ————————-”
“Computer Name: “+ $strComputer
“Caption: ”+ $objItem.Caption
“CPU Status: ”+ $objItem.CpuStatus
“Current Clock Speed: ”+ $objItem.CurrentClockSpeed
“Device ID: ”+ $objItem.DeviceID
“L2 Cache Size: ”+ $objItem.L2CacheSize
“L2 Cache Speed: ”+ $objItem.L2CacheSpeed
“Name: ”+ $objItem.Name
“System Name:” + $objItem.SystemName

}
}

Function DiskInfo {
$colItems = Get-WmiObject Win32_DiskDrive -Namespace “root\CIMV2″ -ComputerName $strComputer
foreach($objItem in $colItems) {
“————————- Disk Info ——————–”
“Computer Name: “+ $strComputer
“Description: ”+ $objItem.Description
“Device ID: ”+ $objItem.DeviceID
“Interface Type: ”+ $objItem.InterfaceType
“Media Type: ”+ $objItem.MediaType
“Model: ”+ $objItem.Model
“Partitions: ”+ $objItem.Partitions
“Size: ”+ $objItem.Size
“Status: ”+ $objItem.Status

}
}

Function NetworkInfo {
$colItems = Get-WmiObject Win32_NetworkAdapterConfiguration -Namespace “root\CIMV2” -ComputerName $strComputer | where{$_.IPEnabled -eq “True”}
foreach($objItem in $colItems) {
“—————————— Network Info ———————”
“Computer Name: “+ $strComputer
“DHCP Enabled:” + $objItem.DHCPEnabled
“IP Address:” + $objItem.IPAddress
“Subnet Mask:” + $objItem.IPSubnet
“Gateway:” + $objItem.DefaultIPGateway
“MAC Address:” + $ojbItem.MACAddress
}
}
Function SysInfo {

$colItems = Get-WmiObject Win32_ComputerSystem -Namespace “root\CIMV2” -ComputerName $strComputer
foreach($objItem in $colItems) {
“—————————— Sys Info ———————”
“Computer Name: “+ $strComputer
“Computer Manufacturer: ” + $objItem.Manufacturer
“Computer Model: ” + $objItem.Model
“Total Memory: ” + $objItem.TotalPhysicalMemory + “bytes”
}
}

# Create a GUI Form
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = “Data Entry Form”
$objForm.Size = New-Object System.Drawing.Size(800,600)
$objForm.StartPosition = “CenterScreen”

$objForm.KeyPreview = $True
#$objForm.Add_KeyDown({if ($_.KeyCode -eq “Enter”)
# {$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq “Escape”)
{$objForm.Close()}})

# Add a drop-down Combo box
$objComboBox = New-Object System.Windows.Forms.Combobox
$objComboBox.Location = New-Object System.Drawing.Size(250,250)
$objComboBox.Size = New-Object System.Drawing.Size(280,20)
$objComboBox.Text = “Please select”

# Add items to Combo box
$commands = @(“SysInfo”,”BIOSInfo”,”OSInfo”,”CPUInfo”,”DiskInfo”,”NetworkInfo”)
ForEach ($command in $commands){
$objComboBox.items.add($command)
}

# Add combo box to Userform
$objForm.Controls.Add($objComboBox)

# Set to the first item
$objComboBox.SelectedIndex = 0;

# Set selection to ComboBox selected text.
$selection = $objComboBox.Text;
write-host $selection

# Add a text box to the Userform
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(250,200)
$objTextBox.Size = New-Object System.Drawing.Size(280,20)
$objForm.Controls.Add($objTextBox)

# Add an OK button and name it as “Submit”
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(250,400)
$OKButton.Size = New-Object System.Drawing.Size(75,25)
$OKButton.Text = “Submit”
$objForm.Controls.Add($OKButton)

# Add a Cancel button
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(350,400)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = “Cancel”
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

# Add an Exit button
$ExitButton = New-Object System.Windows.Forms.Button
$ExitButton.Location = New-Object System.Drawing.Size(450,400)
$ExitButton.Size = New-Object System.Drawing.Size(75,23)
$ExitButton.Text = “Exit”
$ExitButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($ExitButton)

# Add a Text label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(250,180)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = “Please enter a comma separated list of servers:”
$objForm.Controls.Add($objLabel)

# Add a Text label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(260,100)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = “Windows Resource Management Tool”
$objForm.Controls.Add($objLabel)

# Set form on top
$objForm.Topmost = $True

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

$x = $objTextbox.text
$vals = $x.split(“,”)
foreach ($strComputer in $vals){
write-host $strComputer
}

$OKButton.Add_Click({

switch ($objComboBox.SelectedItem)
{
“SysInfo” {SysInfo}
“BIOSInfo” {BiosInfo}
“OSInfo” {OSInfo}
“CPUInfo” {CPUInfo}
“DiskInfo” {DiskInfo}
“NetworkInfo” {NetworkInfo}

}
$objForm.Close()})

# Set selection to ComboBox selected text.
$selection = $objComboBox.Text;
write-host $selection
foreach ($strComputer in $vals){
switch($selection) {
“SysInfo” { SysInfo }
“BIOSInfo” {BiosInfo}
“OSInfo” {OSInfo}
“CPUInfo” {CPUInfo}
“DiskInfo” {DiskInfo}
“NetworkInfo” {NetworkInfo}
}
}

$x

Related posts
1. Stir fry a VBA application quickly
2.Building a respectable VBA with Excel application
3. Get your feet wet with Powershell GUI
4. Powershell GUI – Adding bells and whistles
5. Slicing and dicing with LogParser & VBA
6. Adventures in LogParser, HTA and charts.

Also see
Brewing a potion with Bluemix, PostgreSQL, Node.js in the cloud
A Bluemix recipe with MongoDB and Node.js A Cloud medley with IBM Bluemix, Cloudant DB and Node.js
– A crime map of India in R: Crimes against women
– What’s up Watson? Using IBM Watson’s QAAPI with Bluemix, NodeExpress – Part 1
– Bend it like Bluemix, MongoDB with autoscaling – Part 1
– Analyzing cricket’s batting legends – Through the mirage with R
– Masters of spin: Unraveling the web with R

Find me on Google+

8 thoughts on “Get your feet wet with Powershell GUI

  1. Hi i’am very new to this i am trying to create form that will ask for computer name and has a one button to install flash and one for java

    Any help will apreciated

    thanks in advance

    Like

  2. I suggest you create a downloadable file for this example The web formatting inserts numerous non-ascii characters (e.g. formatted double quotes) that aren’t recognized by powerShell.

    Like

Leave a comment