Solved

Unable to start / save state hyper V machine with script

Posted on 2010-09-02
1
1,570 Views
Last Modified: 2013-11-08
Hi,

I'm trying to start or save the state of an Hyper V vritual machine. I tried several scripts like:

Option Explicit

Dim WMIService
Dim VMList
Dim VMName

'Specify the name of the virtual machine that I want to start
VMName = "ITGSRV053"

'Get instance of ‘virtualization' WMI service on the local computer
Set WMIService = GetObject("winmgmts:\\.\root\virtualization")

'Query for the specific virtual machine that I want to start
Set VMList = WMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName='" & VMName & "‘")

' Request a state change on the first VM that is returned
' 2 = start, 3 = stop and 32769 = save state
VMList.ItemIndex(0).RequestStateChange(2)

or

http://pshyperv.codeplex.com/

When i run the pshyper v command sometimes they work but most of the time i get no output and nothing happens. When i run the VBS script above under an elevated cmdline i get the following error:
C:\Scripts\Test.vbs(18, 1) SWbemObjectSet: Invalid query

line 18 is: VMList.ItemIndex(0).RequestStateChange(2)

Any advice on this one ?

 
0
Comment
Question by:robbe
1 Comment
 
LVL 6

Accepted Solution

by:
robbe earned 0 total points
ID: 33586741
I managed to fix it myself by using the following script:

#The name of the virtual machine to be started
$VMName = "Windows Server 2003"
 
#Get the VM Object
$query = "SELECT * FROM Msvm_ComputerSystem WHERE ElementName='" + $VMName + "'"
$VM = get-wmiobject -query $query -namespace "root\virtualization" -computername "."
 
#Request a state change on the VM
$Result = $VM.RequestStateChange(2)

and

#The name of the virtual machine to be saved
$VMName = "ITGSRV053"
 
#Get the VM Object
$query = "SELECT * FROM Msvm_ComputerSystem WHERE ElementName='" + $VMName + "'"
$VM = get-wmiobject -query $query -namespace "root\virtualization" -computername "."
 
#Request a state change on the VM
$Result = $VM.RequestStateChange(32769)

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now