How Start or Shutdown multiple VM's with Powershell in particular order

Sometimes need to use Powershell instead of GUI in HYPER-V Host. For users that want to learn more Powershell or for those that has a Server Core installation for the HYPER-V Host.

Don't forget that in some cases maybe want to automate a process with Powershell script

Today i would like to talk about a simple command that can use to Shutdown or Start a Virtual Machine from Powershell.

If you read this article you have already setup a HYPER-V Environment and probably you have a lot of Virtual Machines. 

The following video it's a very good resource to learn or guide you in the right way for the HYPER-V implementation details

> Video : Implementing Windows Server 2016 Hyper-V by Greg Shields (MVP)

Let's start !!!!

Shutdown a Virtual Machine

Here is a simple command that can use from the Powershell to shutdown a Virtual Machine.

Stop-VM -Name <VMname>

 

Start a Virtual Machine

And now it's time to start a Virtual machine from Powershell

Start -VM -Name <VMname>

 

Start or Shutdown Virtual Machine in particular order

How can automate this process? How can create the script to run the command in particular order.

Let's talk about workflow sequence in powershell.

First of all Powershell Workflows is available from Powershell version 3..

Workflow is a sequence of automated steps or activities that can execute tasks. It sounds very interesting.

The benefits of a workflow over a simple script is that can perform an actions against multiple devices . A Powershell Workflow is a powershell script that leverages Windows Workflow foundation.

When to use a Powershell Workflow instead of a script or function?

  • You need to perform a long-running task that include multiple steps in a sequence.
  • When you want to run a  task on multiple devices.
  • When you want to run a task that requires checkpointing or persistence.
  • You need to perform a long-running task that is asynchronous, restartable, parallelizable, or interruptible.
  • You need to run a task on a large scale, or in high availability environments, requiring throttling and connection pooling.

I will not explain more details about Powershell Workflows but you can find  more details in the following links 

Windows PowerShell Workflow Concepts from Microsoft PowerShell Documentation

PowerShell Workflows: The Basics  from Hey, Scripting Guy! Blog

 

In this scenario I will try to give you an example how can use a basic powershell Wokflow.

Let's say that you want to start multiple Vm's in particular order. How can do that?

One way is to use a function with lot of lines of code to do it.

Another way is to use a workflow with few lines to run the script as you want.

Let's take a look in workflow script

workflow startvms

{

sequence

{

Start-vm -name dc1

Start-vm name mdt

Start-vm name bckserver

}

}

startvms

 

If you want to shutdown multiple vm's you can use the same workflow and will change the Start-VM with Stop -VM

workflow stopvms

{

sequence

{

Stop-vm -name dc1

Stop-vm name mdt

Stop-vm name bckserver

}

}

stopvms

That's it

I hope to find my article valuable and help you in your daily tasks.

I invite you to follow me on Twitter , Google+ or Facebook. If you have any questions, send email to me at info@askme4tech.com.

Have a nice day !!