Already seen this part? Check out Part 1, Part 2, and Part3
Powershell, while a powerful tool can sometimes lack that person touch that a GUI interface offers. Console output is fine and dandy, but there are times when parsing through a large amount of data can be done with a GUI and some simple commands.
From here, I’m going to assume you know the basics. Such as awareness of the get-executionpolicy and set-executionpolicy commands, and a little knowledge of PowerShell ISE.
The Setup
- Windows 10
- PowerShell ISE
- https://www.microsoft.com/en-ca/download/details.aspx?id=45520
- A working Domain with read access
- POSHGUI.com website
I’m doing this with my home lab from a non-domain computer. Which means I’ll be using some credential switches you may not need.
For this example, we’ll take something simple, like
Get-ADUser
Get-ADUser gets a user object or searches to get multiple user objects from Active Directory. To keep this simple, we’ll have it search up a name. You’ll get the idea of what I’m doing and how we can keep it somewhat simple.
First, we’ll get a simple query working. Let’s search for a user named ‘Rick’
get-Aduser -Filter {name -like '*rick*' } -Server 10.0.0.101 -Credential "contoso.com\administrator"
Explanation of the above query:
-Filter {name -like '*rick*'}
Search for a username using wildcards in-front and behind of name we’re looking for.
-Server 10.0.0.101
Look at a specific Domain controller. Since this is on a different subnet for me, I’m specifying it by IP. You can simply use the hostname of the domain controller, or you can use “-SearchBase” and plug in the domain name itself.
-Credential "contoso.com\Administrator"
Provide a specific set of credentials with read access to the domain. Since I’m issuing these commands from an non-domain machine, this is handy for me in the testing phase.
Put this or something very close to this into PowerShell ISE. Highlight the code and hit F8 to run the selection. This just ensures you have something to search for. Obviously, I chose ‘Rick’ because that name exists in my Domain, you’ll have to pick or create one to search.
So now we have the format of the query, how can we turn it into a GUI?
Visit POSHGUI.com. This is a free website that creates the Powershell forms for you. All you need to do is create the code to plunk in. Goto WinForms Designer.
Now on the form drag a button and a text box. For this example, we’ll want the text box a little bigger – which means in the ‘behavior’ section, we need multi-line. You should have something similar to the below:
I kept the default values for each. They should be button1 and textbox1 respectively from here on.
What we want to happen: When pressing ‘Search Up “Rick”‘ button, the results should show in the text box below.
In PoshGUI click on the ‘Code’ button. This creates the entire form with coordinates, arguments and ranges.
The above is a screenshot, I’ve added my code below with some additional explanation in between the comments.
$results = $null #reset the $results variable each run
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = New-Object System.Drawing.Point(400,324)
$Form.text = “Form”
$Form.TopMost = $false
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = “Search up `”Rick`””
$Button1.width = 253
$Button1.height = 30
$Button1.location = New-Object System.Drawing.Point(62,14)
$Button1.Font = New-Object System.Drawing.Font(‘Microsoft Sans Serif’,10)
<#—–Calls a function ‘ADSearch’——-#>
$Button1.Add_MouseClick({ADSearch})
$TextBox1 = New-Object system.Windows.Forms.TextBox
$TextBox1.multiline = $true
$TextBox1.text = “Results show up here”
$TextBox1.width = 338
$TextBox1.height = 261
$TextBox1.location = New-Object System.Drawing.Point(26,57)
$TextBox1.Font = New-Object System.Drawing.Font(‘Microsoft Sans Serif’,10)
$Form.controls.AddRange(@($Button1,$TextBox1))
[system.windows.forms.application]::run($form) #Runs form with runtimes – this is a trial and error thing
Function ADSearch {
<#——function ‘ADSearch’ runs Get-ADUser query, stores into $results variable——#>
$results= get-Aduser -Filter {name -like ‘*rick*’ } -Server 10.0.0.101 -Credential “contoso.com\administrator”
<#—–Display output to $Textbox1.text——-#>
$TextBox1.text = $results
}
Copy and paste that code, edit the query to whatever name exists in your Domain, and the IP to match whatever domain IP you have. Hit F5 to run the script, and you should see something similar.
Click the Mouse Button, enter your domain credentials and the results display:
And that’s about it! It’s a simple script, and shows some of the very simple ways to display information in a GUI. This only needs PowerShell ISE, PoshGUI, and some curiosity.
If you want to make this a little fancier, take input from a user, and display specific information, join me for Part 2.
[ivory-search 404 "The search form 3350 does not exist"]