So far in this article series, we have created a script that lets the user pick a virtual machine, and then displays information about that VM. That’s all well and good, but the script would be much more useful if it could be used to actually do something other than passively displaying information. In this article, we will add some functionality to the script.

My goal in this article is to create a toggle switch that can be used to start or stop a virtual machine. Now there is no such thing as a toggle switch object in PowerShell, so we have to be a little bit creative. My plan is to create a pair of buttons – one to start a VM, another to stop the VM. I can then use some simple logic to control which button is displayed. If the selected VM is currently running, I will display the Stop button. If the VM is currently stopped, I will display the Start button.

So the first thing that I want to do is to create a pair of buttons. This is really simple to do. As you may recall, the script already contains four button objects. I’m going to create two more. Here is a sample code block:

Right now these buttons don’t do anything, but I’m about to change that. Before I do, I want to show you what the buttons look like if I add them to the form. Notice in Figure A, that I have colored the buttons red and green, with white text. I didn’t have to color the buttons, but I went ahead and used color so that you could see how the BackColor and ForeColor attributes work. You can see how these attributes are configured in the code block above.

Figure A

I have created a Start and Stop button.

So now that the buttons have been created, let’s build some logic that will display either one button or the other, depending on whether or not the VM is running. This involves doing three things.

First, we need to create a pair of If statements that add the buttons to the interface depending on the state of the virtual machine. As you may recall, we previously created a function called Display-VMInfo that displays all of the virtual machine information shown in the figure above. We can easily add the two if statements to this function. Here is the portion of the function that displays the screen contents:

As you can see in the code block above, if the virtual machine is off, then Button 5 (Start VM) will be displayed. Likewise, if the virtual machine is running then Button 6 (Stop VM) will be displayed.

The second thing that we need to do is to add some code to remove the buttons from the interface wherever it is appropriate to do so. For example, the Return-MainScreen function contains code to remove various graphical elements from the screen. We need to add lines to remove Button5 and Button6. You can see what that code looks like below:

Finally, we should probably change the button location for button 6 so that the button displays in the same place on the screen as button 5. Remember, the two buttons will never be on the screen at the same time. Instead, the goal is to create the illusion of a toggle switch that we can use to turn the VM on or off. Here is what the location command for Button 6 should look like:

$Button6.Location = New-Object System.Drawing.Size(140,260)

So now let’s take a look at how the script works. If you look at Figure B, you can see that the VM is currently powered off, and the GUI presents an option to start the VM.

Figure B

The GUI provides the option to start the VM.

Likewise, Figure C shows an example of a VM that is running. Because the VM is currently powered on, there is a button that we can use to stop the VM.

Figure C

The GUI provides an option to stop a running virtual machine.

As it stands right now, the buttons don’t actually do anything. We need to change the button action for the Start and Stop button to point to a function that can change the VM state. There are a few different ways of handling this. We could create a single function that re-checks the state of the VM, and then uses If statements to either start or stop the VM. The code used for doing so would be very similar to the code used to display the Start and Stop buttons.

Another, slightly simpler approach would be to create two different functions, one to start the VM, and one to stop the VM. That way, each of the two buttons calls a very simple, dedicated function.

The easiest method of all is to just build the Start-VM and Stop-VM cmdlet directly into the button click actions. In order to do so, there is one change that must be made to the function. Right now, the Display-VMInfo function contains the following line of code, which assigns the virtual machine name to a variable called $Name:

$Name = $SelectedVM.VMName

We need to make $Name a global variable so that it will be recognized outside of the function. Remember, out button click action is defined in the script’s main body, where $Name is currently unrecognized. We can change $Name into a global variable by modifying the variable assignment to look like this:

$Global:Name = $SelectedVM.VMName

Now, we can set the button click actions to:

$Button5.Add_Click({Start-VM $Name})

And

$Button6.Add_Click({Stop-VM $Name})

This technique works pretty well, but it’s not quite perfect, at least not as written. As it stands right now, clicking the Start VM button will indeed start the VM, but the display is not refreshed to reflect the new status. Clicking Stop-VM causes the PowerShell window to stop and wait for confirmation before shutting down the virtual machine. Hence, the shutdown process is not truly automated. So here is how we fix these problems.

We can fix the shutdown problem by appending the -Force parameter to the Stop-VM command. We can then fix the screen refresh issue by appending the pipe symbol to both of the click actions, and then calling the Display-VMInfo function. When doing so, be sure to pass the $Name variable to the function. You will also need to add instructions to the beginning of the function to remove buttons 5 and 6 so that the buttons will display correctly.

Conclusion

In this article, I have shown you how to add a button that toggles the virtual machine state. In the next article in the series, I want to show you how use a script to modify the virtual machine’s memory configuration.

Post Views: 555

Featured Links

Brien Posey

Brien Posey is a freelance technology author and speaker with over two decades of IT experience. Prior to going freelance, Brien was a CIO for a national chain of hospitals and healthcare facilities. He has also served as a network engineer for the United States Department of Defense at Fort Knox. In addition, Brien has worked as a network administrator for some of the largest insurance companies in America. To date, Brien has received Microsoft’s MVP award numerous times in categories including Windows Server, IIS, Exchange Server, and File Systems / Storage. You can visit Brien’s Website at: www.brienposey.com.

Featured Product

Featured Product

Latest Podcast

Featured Freeware

Recommended

Follow Us

Creating a PowerShell GUI (Part 12)

TECHGENIX

TechGenix reaches millions of IT Professionals every month, and has set the standard for providing free technical content through its growing family of websites, empowering them with the answers and tools that are needed to set up, configure, maintain and enhance their networks.