?
Solved

Unable to start / save state hyper V machine with script

Posted on 2010-09-02
1
Medium Priority
?
1,590 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

762 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