PowerShell Studio 2012 - WinForms - GUI ToolMaking
In my previous post I showed how to create a quick PowerShell GUI to append some colored text in a RichTextBox control using Sapien PowerShell Studio 2012.
Today I will go a bit further and show you how to create a tool to query some information from a remote computer. I will first send the Output to Out-GridView cmdlet and then show you how to send it to a DataGridView control inside the GUI.
The tool will query Services, Processes and Shares from a Remote Computer. You will need to specify the ComputerName and the Credential required to perform those actions. The goal is to show how to create something very simple so I did not write any Error Handling or any conditional code in this version.
One cool thing to mention when using PowerShell Studio 2012 is, if you add some controls like a DataGridView, a Listview or a ListBox, PowerShell Studio 2012 will add some functions to help you Load/Add/Refresh those controls. I will show you below in the part “Replacing the Out-Gridview by a DataGridView Control”
Overview
-
ToolMaking - Video
-
ToolMaking - Step By Step
-
Creating and Editing the GUI
-
Adding your PowerShell Code
-
Test the GUI, using Out-Gridview cmdlet for the output
-
Replacing the Out-Gridview by a DataGridView Control
-
Modifying your PowerShell code for DataGridView
-
Test the GUI using the DataGridView for the output
-
Download (PS1 and PFF)
ToolMaking - Video
Creating and Editing the GUI
Insert your controls. Here I used 3 buttons and one TextBox Rename your button text properties and rename the textbox control (Design Name property) so it will be easier to find it when writing the PowerShell code.
Adding your PowerShell Code
Here is the code I used for each of my events. I basically do some basic Get-WmiObject queries on different classes related to the Services/Processes and Shared. Note the Output is sent to the pipeline to Out-GridView.</b>
$buttonCredential_Click={
# Ask for Credential
$global:cred = Get-Credential -Credential 'FX\Administrator'
}
$buttonServices_Click={
# Query the Services on the computername specified in the textbox
Get-WmiObject Win32_Service -ComputerName $textboxComputerName.Text -Credential $cred | Select-Object __Server,Name | Out-GridView
}
$buttonProcesses_Click={
# Query the Processes on the computername specified in the textbox
Get-WmiObject Win32_Process -ComputerName $textboxComputerName.Text -Credential $cred | Select-Object __Server,Name | Out-GridView
}
$buttonShares_Click={
# Query the Shares on the computername specified in the textbox
Get-WmiObject Win32_Share -ComputerName $textboxComputerName.Text -Credential $cred | Select-Object __Server,Name | Out-GridView
}
Test the GUI, using Out-Gridview cmdlet for the output
Here is the result when you click on one of the 3 buttons. Each of them invoke the events:
-
$buttonServices_Click
-
$buttonProcesses_Click
-
$buttonShares_Click
You can define all sort of Events for each control, take a look below
Replacing the Out-Gridview by a DataGridView Control
Expend your Form and add a DataGridView control.
Leave a comment