Creating GUI Front End for Powercli Scripts

While working with the team on some powercli projects I was asked to try and make the scripts more user friendly to use. The group was wanting a GUI front end for the scripts. I did some research and ran accross a series of articles on using Visual Studio to create a WPF form. Here is a link to the blog series.

This was exactly what I was needing. I downloaded the latest version of Visual Studio 2019 Community Edition and away we go. I haven't used Visual studio in years, so I was expecting this to be really heard. After a few minutes of reading I was building a form. this post was the most helpful.

Here is a sample picture of my form

After the form was complete I copied all the code at the bottom into Powershell ISE. One note to add. I created a image box on line 12. if you want to logo you form add the location where the logo is located here and it will appear.

All that is needed to make this work in Powershell/Powercli is to add the lines as shown at the top and bottom of the image. At the top add inputXML = @"

then all the code for your form. finally close with a "@

The next step was to load all the XAML objects in to Powershell. see the image below. Part 2 of the series I recommended goes into alot more detail.

Next I added the Move-CrossVC function into the new script. there are several variables in this function to make it work. The next challenge was to get the form information into the variables needed to make the function work. Above in the picture of the code, you see the Get-formVariables function. this creates a variable out of all the text boxes in this example. It adds a $WPF in front of the x:Name of the text box. So if you saw a recent post on the Move-CrossVC script I created the variabled changed to look like the following

Be sure to add the .text to the end of the new variable name to get the info entered into the text box. The last piece is to get the buttons at the bottom of the form to work. here is the code used to do that. In this Form there are 2 buttons. a Migrate button and a Cancel button. Once the button code is in place be sure to finish the script with the line $Form.ShowDialog() | out-null or the form will not work.  Below is a snip of the code to accomplish this.

Once the script starts working, the powershell window will show the progress. There will be updates to this post as more features are added to the form. Please stay tuned. I put a finished product out on my github if anyone is interested, link below.

If you havent already, follow me on twitter. I tweet the updates and new blog post there.