?
Solved

Unable to start / save state hyper V machine with script

Posted on 2010-09-02
1
Medium Priority
?
1,617 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

840 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