Good morning all,
Tried posting this in the scripting section, but I was advised to try here. Using a combination of the WMI Code Creator example, combined with the method to get the VM object from Microsoft's root\virtualization\ example of ApplyVirtualMachineSnapshot, (no VBScript example for V2)
https://msdn.microsoft.com/en-us/library/cc136767(v=vs.85).aspx
I am trying to put together a VBScript to apply a snapshot, but I keep getting errors 32773 (invalid parameter) or 32775 (invalid state). I know the VM and snapshot are in a state that can be applied, because I can do it from the Hyper-V Manager console, but no matter what property I try fails with one of those two errors. Passing the InParam as ".Path" ".InstanceID" ".ElementName" or anything else results in error 32773, but ".Path_.Path" gives a 32775 which makes me think that is the right parameter. Why wrong state error?
This one is driving me insane. I would appreciate any help in figuring this out. The full script is below. Thank you in advance!
Option Explicit
Dim strComputer, strVM
Dim objWMIService, objShare, objItem, objVM
Dim SnapList, Snap, SnapShot, VMList
Dim objInParam, objOutParams
'On Error Resume Next
' Strings will be passed as arguments once this is working
strComputer = "<HVServer>"
strVM = "<VMElementName>"
' Connect to host server's WMI
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\virtualization\v2")
' Obtain an instance of the the class using a key property value.
Set objShare = objWMIService.Get("Msvm_VirtualSystemSnapshotService.CreationClassName='Msvm_VirtualSystemSnapshotService',Name='vssnapsvc',SystemCreationClassName='Msvm_ComputerSystem',SystemName='" & strComputer & "'")
' Get VM object from function
Set objVM = GetComputerSystem(strVM)
' Just for testing sake, grab all snapshots and match to snapshot name
Set SnapList = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemSettingData WHERE VirtualSystemType = 'Microsoft:Hyper-V:Snapshot:Realized'")
For Each Snap In SnapList
If inStr(Snap.ElementName,strVM) Then
msgBox Snap.Path_.Path
' Successfully finding snapshot object here
Set SnapShot = Snap
End If
Next
If (Err.Number <> 0) Then
msgBox "Err.Number: " & Err.Number & vbCrLf & _
"Err.Description: " & Err.Description, _
0+16,"Error!"
WScript.Quit(1)
End If
' Obtain an InParameters object specific to the method.
Set objInParam = objShare.Methods_("ApplySnapshot").inParameters.SpawnInstance_()
objInParam.Properties_.Item("Snapshot") = SnapShot.Path_.Path
' Execute the method and obtain the return status.
' The OutParameters object in objOutParams is created by the provider.
Set objOutParams = objWMIService.ExecMethod("Msvm_VirtualSystemSnapshotService.CreationClassName='Msvm_VirtualSystemSnapshotService',Name='vssnapsvc',SystemCreationClassName='Msvm_ComputerSystem',SystemName='" & strComputer & "'","ApplySnapshot", objInParam)
' List OutParams
msgBox "Out Parameters: " & vbCrLf & _
"Job: " & objOutParams.Job & vbCrLf & _
"ReturnValue: " & objOutParams.ReturnValue, _
0+16,"Error!"
'-----------------------------------------------------------------
' Retrieve Msvm_VirtualComputerSystem from base on its ElementName
'-----------------------------------------------------------------
Function GetComputerSystem(vmElementName)
Set VMList = objWMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem WHERE Caption = 'Virtual Machine'")
For Each objItem In VMList
If UCase(objItem.ElementName) = UCase(vmElementName) Then
Set GetComputerSystem = objItem
End If
Next
If (Err.Number <> 0) Then
msgBox "Err.Number: " & Err.Number & vbCrLf & _
"Err.Description: " & Err.Description
WScript.Quit(1)
End If
End Function