Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Use PowerShell to Start or Stop Virtual Machines in Order

$
0
0

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using a Windows PowerShell workflow to start or stop virtual machines in a specific order.

Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sitting outside enjoying a beautiful fall day. I found a little coffee and tea shop nearby, so I am sitting outside sipping a nice cup of English Breakfast tea and munching an apple cinnamon scone. It is not too bad actually. Of course, the little coffee and tea shop has WiFi, so I have my Surface 3 Pro with me, and I am checking email sent to scripter@microsoft.com. I ran across an email from a person who is trying to start various virtual machines in a particular order on the server running Hyper-V. Hmmm…That sounds like a job for a Windows PowerShell workflow.

Note  For a good review of Windows PowerShell workflow, refer to this collection of Hey, Scripting Guy! Blog posts. I have nearly twenty posts about workflow, so I suggest that you begin with the “Workflow for Mere Mortals” series.

Windows PowerShell workflow sequence

Using a Windows PowerShell workflow sequence is really an easy thing to do. Creating a Windows PowerShell workflow is actually no more difficult than creating a Windows PowerShell function. In fact, instead of using the word function, I use the word workflow. That is about it.

Oh, one more thing...

I need to know how to create curly braces (or brackets, or tuborgs, or curly fries...whatever you like to call the characters that are above the left and right square brackets on an EN-US keyboard). The left brace is ASCII 123 and the right brace is ASCII 125. OK. That is the most difficult portion.

I begin my workflow, provide a name for the workflow, and open the script block by typing the left brace. Here is what this looks like:

workflow start-vms

{

Now, I use the Sequence keyword to indicate that I want to perform the tasks in a specific order. The Sequence keyword also uses a script block, so it opens with another left curly brace. Here is what this looks like:

Sequence

        {

Now all I need to do is include the Start-VM commands in the order in which I want my virtual machines to start. This is shown here:

         Start-vm -Name dc1_nwt

         Start-VM -Name S1_nwt

         Start-VM -Name S2_nwt

         Start-VM -Name C1_nwt 

Now, I need to close the script blocks and call the workflow. Here is that portion of the script:

  }

}

start-vms

The complete script is shown here:

workflow start-vms

{

    Sequence

        {

         Start-vm -Name dc1_nwt

         Start-VM -Name S1_nwt

         Start-VM -Name S2_nwt

         Start-VM -Name C1_nwt

         }

}

start-vms

I run the script, and a progress bar appears for each virtual machine, as shown here:

Image of command output

Stopping the virtual machines

To stop the virtual machines in a specific order, all I need to do is find the word Start and replace it with Stop, then reorder the machines. The completed script is shown here:

workflow Stop-VMS

{

    Sequence

        {

         Stop-VM -Name C1_nwt

         Stop-VM -Name S2_nwt

         Stop-VM -Name S1_nwt

         Stop-VM -Name DC1_nwt

         }

}

Stop-VMS

When I run this, once again, the progress bar appears to let me know how the script is going:

Image of command output

That is all there is to using Windows PowerShell to start and to stop virtual machines in a specific order. Workflow Week will continue tomorrow when I will talk about more cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 


Viewing all articles
Browse latest Browse all 3333

Trending Articles