Featured image of post The PowerShell Box Of Tricks GUI

The PowerShell Box Of Tricks GUI

When I started as a DBA at MyWork I faced a challenge. Many hundreds of databases, dozens of servers and no idea what was on where. It was remembering this situation when new team members were appointed that lead me to write the Find-Database script and I had written a simple GUI using Read-Host to enable the newbies to see the functions I had created

image

Whilst writing this series of posts I decided that I would create a new GUI

image

I wanted the choice to be made and then the form to close so I had to use a separate function for calling all the functions referenced in the form. This function takes an input $x and depending on the value runs a particular code block. Inside the code block I ask some questions using Read-Host to set the variables, load the function and run it as shown below for Show-DriveSizes

image

Then I set about creating the GUI. First we load the Forms Assembly, create a new Form object and add a title

image

Then using the details found here I I converted the image to ASCI and use it as the background image and set the size of the Form

image

I choose a default font for the form. Note there are many many properties that you can set for all of these objects so use your best learning aid and find the ones you need.

image

I then create three labels. I will show one. I think the code is self-explanatory and you will be able to see what is going on. Don’t forget to the last line though! That adds it to the form, if you miss it you can spend a few minutes scratching your head wondering why it hasn’t appeared!!!

image

We need a Text Box for the User to put their choice in. Again the code is fairly easy to understand

image

The next bit of code enables the user to use Enter and Escape keys to Go or to Quit. Notice that both call the Close() method to close the Form and return to the PowerShell console

image

Add a button for OK and one for quit

image

and finally Activate the Form, Show it and run the function to call the correct function

image

The Return-Answer function simply calls the Return-Function function. I am not sure if that is the best way of doing it but it works in the way i wanted it to

image

Built with Hugo
Theme Stack designed by Jimmy