Solved

Unable to start / save state hyper V machine with script

Posted on 2010-09-02
1
1,578 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Read WSUS log on Window 10 5 79
powershell and select-string regex multi line 8 42
Open new WPF windows - powershell 4 32
invoke-sqlcmd help 5 27
This article will help you understand what HashTables are and how to use them in PowerShell.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

738 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