Automate Hyper-V with VBScript or Powershell

By Rick Vanover
Friday, 26 June, 2009


To effectively manage any virtualisation platform, you need a strong arsenal of scripting tools. This can allow various levels of automation and shorten configuration time. Microsoft's hypervisor includes scripting options that can use VBScript and the robust PowerShell for Hyper-V script environment. In this tip, I outline how Hyper-V scripting works and show a few examples of how to get started .

Hyper-V's scripting environment is officially called the Windows Management Instrumentation (WMI) , and we will focus on the virtualisation namespace. With a WMI interface, administrators have scripts for a wide range of tasks with Hyper-V. And the options for admins get even better with the PowerShell Management Library.

Deciding what script to use depends largely on what is required and what is available. I will focus on Hyper-V servers that are not centrally managed with System Center Virtual Machine Manager (SCVMM), because Microsoft provides additional scripting options for SCVMM implementations. Check out this TechNet page for an overview of SCVMM scripting options.

Microsoft is better at writing scripts
My career has revolved around scripting as needed and working off scripts that I find on the Internet. Chances are Microsoft has a better script than what you or I can create independently. Luckily, there are plenty of resources to get you started. Below I provide a list of some of the best online resources to get started with Hyper-V scripting:

  • Virtual PC Guy's WebLog: Ben Armstrong's Microsoft Developer Network (MSDN) blog is a great source for sample scripts from all Microsoft virtualisation systems.
  • Taylor Brown's Blog: This is another strong offering for the Microsoft virtualisation camp. Brown covers several use cases, including one for SCVMM.
  • The Microsoft virtualisation team blog site: This is a good catchall for everything related to Hyper-V, as well as other virtualisation segments.
  • PowerGUI.org's Hyper-V PowerPack: This is roughly equivalent to VMware's VI Toolkit and its functionality from PowerGUI is a definite must-have'. Be sure to check out this TechTarget tip by Eric Seibert on this powerful toolkit.

Get VM information example
Now that I've given an overview of the scripting options, let's jump into an example. Nearly every resource for PowerShell scripting with Hyper-V begins with the query script, especially if the script involves deterministic handling. This is a simple one-liner that interacts with the WMI virtualisation namespace of Hyper-V and gives information on a VM as it is exists in the namespace. Many scripts will want to query this to get current status on elements such as the OperationalStatus value, which indicates a VM's run state. Here is a sample command to get this information for a VM named "TESTVM1":

Get-WmiObject -Namespace root\virtualisation -Query "Select * From Msvm_ComputerSystem Where ElementName='TESTVM1'"

The VM name is the only value to change if you want to run it in your own environment. When executed in PowerShell, the result displays, as shown in Figure 1.

Note that the VM's OperationalStatus value is displayed as "2", meaning it is running.

Stop/start a VM with VBScript example

The basic tasks of starting and stopping a VM are good ways to get started with scripting. Other than PowerShell, Hyper-V machines can interact via VBScript and get full access to the WMI virtualisation namespace. This example script performs a startup of a VM that is powered off using VBScript:

Option Explicit

Dim CallWMI

Dim InventoryVMs

Dim YourVM

YourVM = "TESTVM1"

Set CallWMI = GetObject("winmgmts:\\.\root\virtualisation")

Set InventoryVMs = CallWMI.ExecQuery("SELECT * FROM Msvm_ComputerSystem

WHERE ElementName='" & YourVM & "'")

InventoryVMs.ItemIndex(0).RequestStateChange(2)

Like the PowerShell example, the only piece that needs to change in this script is the inline value for "YourVM", which is set with the VBScript variable as "TESTVM1". Saving this text as a .VBS file on the local file system will allow it to execute locally.

Let's now twist this a bit and perform a shutdown on a remote Hyper-V server. The prior example was for running a script locally for a server that has the Hyper-V role and the designated VM running. This can be beneficial in environments with multiple Hyper-V servers and where SCVMM is not implemented. This script will shut down (force power off) the TESTVM1 VM remotely on Server55:

Option Explicit

Dim CallWMI

Dim InventoryVMs

Dim YourVM

YourVM = "TESTVM1"

Set CallWMI = GetObject("winmgmts:\\SERVER55\root\virtualisation")

Set InventoryVMs = CallWMI.ExecQuery("SELECT * FROM Msvm_ComputerSystem

WHERE ElementName='" & YourVM & "'")

InventoryVMs.ItemIndex(0).RequestStateChange(3)

Note that Line 6 enters the name of Server55, which is the Hyper-V server that holds TESTVM1. This can run remotely from systems that are aware of the WMI virtualisation namespace, such as another Windows Server 2008 server. The forced shutdown is sent with the Code 3 in the last line of the above example. Other popular VM codes include the following:

  • Reboot (10): This code performs a hard reset on a VM.
  • Pause (32768): This code pauses the VM.

Complete information on the RequestChangeState method of the WMI virtualisation namespace can be found online at the MSDN website.

Testing Hyper-V scripts

Hyper-V scripting should be done in a test environment. PowerShell is, as advertised, an extremely powerful shell environment. Scripts written in VBScript present the same riskbecause they do what you tell them to do, as they will also do what you tell them to do.. And when commands are passed, there is no Cancel button or backing out of commands. Simply put, the commands assume that you know what you are doing. Also, consider permissions, which could be a concern with remote Hyper-V servers.

On with scripting

There are plenty of options for administrators who want to automate elements of their Hyper-V environments. With some practice, care and patience, you can get Hyper-V tuned to your tastes with PowerShell or VBScripts that you create.

Related Articles

Staying ahead: business resilience in the hybrid cloud era

The rise of cloud computing and advancements in virtualisation have revolutionised how businesses...

Taming cloud costs and carbon footprint with a FinOps mindset

In today's business environment, where cloud is at the centre of many organisations' IT...

The power of AI: chatbots are learning to understand your emotions

How AI is levelling up and can now read between the lines.


  • All content Copyright © 2024 Westwick-Farrow Pty Ltd