Write a GUI on Top of Existing PowerShell Functions with SAPIEN PowerShell Studio 2016
This blog article will demonstrate how to write a GUI on top of your existing PowerShell functions
using SAPIEN PowerShell Studio 2016. I've
previously written a couple of functions for managing SQL Server agent jobs. These two functions,
Get-MrSqlAgentJobStatus
and Start-MrSqlAgentJob
can be found in
my SQL repository on GitHub.
Launch PowerShell Studio. Select file, the arrow next to new, and new form:
For this particular GUI, I'll select the "Dialog Style Template" since I want a fixed border without any minimize or maximize buttons:
One thing to note is the Toolbox auto-hides by default. It can be found on the left side of the screen:
Add another button to the form:
Click on the button on the form itself and them modify the properties in the lower right side of PowerShell Studio:
The text of both buttons and the size of one of them has been changed:
Double-click on the exit button and add the code to close the form:
1$buttonExit_Click={
2 $form1.Close()
3}
Add the existing functions to the project:
Select the functions to add:
You can see that the functions have been added:
They can also be seen (and modified if needed) in the script pane:
Either double-click on the other button from the Designer tab or simply add the button click event handler for it and write the code to wire the exiting functions up to it:
1$buttonRunSQLAgentJob_Click={
2 $Params = @{
3 ServerInstance = 'SQL011'
4 Name = 'test'
5 }
6
7 $result = Start-MrSQLAgentJob @Params
8
9 if ($result -eq $true) {
10 Start-Sleep -Seconds 5
11 while ((Get-MrSQLAgentJobStatus @Params).Outcome -eq 'Executing') {
12 $buttonRunSQLAgentJob.Text = 'In Progress'
13 $buttonRunSQLAgentJob.BackColor = 'Orange'
14 Start-Sleep -Seconds 20
15 }
16
17 if ((Get-MrSQLAgentJobStatus @Params).Status -eq 'Succeeded') {
18 $buttonRunSQLAgentJob.Text = 'Success'
19 $buttonRunSQLAgentJob.BackColor = 'Green'
20 }
21 else {
22 $buttonRunSQLAgentJob.Text = 'Error!'
23 $buttonRunSQLAgentJob.BackColor = 'Red'
24 }
25
26 }
27 else {
28 $buttonRunSQLAgentJob.Text = 'Error!'
29 $buttonRunSQLAgentJob.BackColor = 'Red'
30 }
31}
The button simply calls the functions as needed and performs the necessary logic based on the results.
To run this GUI as a user who has been given access to run the SQL agent jobs without actually giving those rights to their user accounts, select "Settings" underneath Package:
On the Output Settings tab, set the Run Mode to "RunAs user" and enter the user name and password information for that account. This account should be setup with the privileged of least principle just in case someone is able to reverse engineer the executable and retrieve the credentials.
Select "Build" underneath package to create an executable:
Run the executable and then click the "Run SQL Agent Job" button:
The SQL agent job is running:
It completed successfully:
The GUI shown in this blog article is a proof of concept and certainly isn't meant to be a fully functional application. It's meant to show how to write a GUI to leverage your existing PowerShell functions so you don't have to reinvent the wheel. If you were writing a GUI to accomplish this task, you would at least want to first check to see if the job is running and disable the button to run it while the job is running.
Don't own PowerShell Studio? Download the 45 day eval.
µ