Creating a Simplistic GUI Interface with Out-GridView
A couple of weeks ago I noticed a post in the forums at PowerShell.org where a question was asked about selecting a group of servers in Server Manager and running a PowerShell script against that selection of machines. While my solution doesn't accomplish exactly what I think they were looking for, it does the next best thing.
First, I added a menu item to Server Manager called MrToolkit (short for Mike Robbins Toolkit):
The item I added is a .bat file that contains the following code:
1powershell.exe -ExecutionPolicy Bypass -NoProfile -WindowStyle Hidden -Command "Invoke-Command -ComputerName (Get-ADComputer -Filter {OperatingSystem -like 'Windows Server*' -and enabled -eq $true} | Out-GridView -OutputMode Multiple -Title 'Select Servers to Query:' | Select-Object -ExpandProperty DNSHostName) -FilePath (Get-ChildItem -Path C:\Scripts\*.ps1 | Out-GridView -OutputMode Single -Title 'Select PowerShell Script to Run:' | Select-Object -ExpandProperty FullName) | Out-GridView -Title 'Results' -PassThru"
Notice the tweaks in the previous code for bypassing the execution policy and running it with no
profile. I also had to add the PassThru
parameter to the last Out-GridView
otherwise the command
finished and exited without the user being able to see the results.
Those tweaks aren't necessary if you just want to run the actual PowerShell code:
1Invoke-Command -ComputerName (
2 Get-ADComputer -Filter {OperatingSystem -like 'Windows Server*' -and enabled -eq $true} |
3 Out-GridView -OutputMode Multiple -Title 'Select Servers to Query:' |
4 Select-Object -ExpandProperty DNSHostName
5) -FilePath (
6 Get-ChildItem -Path C:\Scripts\*.ps1 |
7 Out-GridView -OutputMode Single -Title 'Select PowerShell Script to Run:' |
8 Select-Object -ExpandProperty FullName
9) | Out-GridView -Title 'Results'
One thing to note is using the Out-GridView
cmdlet requires the PowerShell ISE to be installed and
Out-GridView
being able to send its results to the pipeline was a feature introduced in PowerShell
version 3 so those are the minimum requirements.
This command first returns a list of all the servers in the domain that are enabled:
After selecting the Servers you want to run a script against and clicking <OK>
as shown in the
previous example, the user is prompted to select one of the scripts that exists in the specified
directory:
The user is limited to selecting one script, after selecting it and clicking <OK>
, the selected
script is run on the remote machines and the results are displayed:
When the user clicks <OK>
, the Out-GridView
window and all traces of the command disappear. How's
that for a simplistic GUI interface for your PowerShell scripts?
µ