Use PowerShell to Start or Stop Virtual Machines in Order


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 

Comments (5)

  1. Hi will the second server wait till first server is stopped(in case of stop-vm script)? or will the stop-vm be initiated in a series without waiting for the previous servers to stop?

    Chaitanya

  2. Thanks Ed, I have seen your recent blogs ok Workflow, and sequence, very helpful.

  3. Susantha Silva says:

    Hi Ed, thank you for sharing this information. Can I know what are the steps I should take if I want to start the VM’s with specific delay before starting the next VM?

    Eg: VM1 start wait 20 seconds then start VM2 ….etc

  4. Designer2002 says:

    This may be too simple, but I would think you could use the Start-Sleep Cmdlet between them.

    Start-Sleep -Seconds 20.

  5. Dmitry Kotov says:

    Hello! Maybe you will be interested in configuring vm startup priority so you can start them all simultaneously and let vmm handle order. Here is link –
    http://onthetrack.weebly.com/blog/using-sma-to-set-vm-boot-priority

Skip to main content