MSDNで公開されているサンプルスクリプトに本当に少しだけ手を加えたスクリプトです。
Hyper-Vで実行中のステータスになっている仮想OSのスナップショットを連続して取得します。
'全ての実行中の仮想マシンのスナップショットを取得します 'CreateALLVirtualSystemSnapshot.vbs option explicit dim objWMIService dim managementService dim fileSystem const JobStarting = 3 const JobRunning = 4 const JobCompleted = 7 const wmiStarted = 4096 const wmiSuccessful = 0 Main() '----------------------------------------------------------------- ' Main '----------------------------------------------------------------- Sub Main() Dim objWMI Dim VM Dim VMList set fileSystem = Wscript.CreateObject("Scripting.FileSystemObject") Set objWMI = GetObject("winmgmts:\\.\root\virtualization") '日本語環境ではホストマシンのDescriptionが'Microsoft ホスト コンピュータ システム'となるのでそれをクエリで除外 Set VMList = objWMI.ExecQuery _ ("SELECT * FROM Msvm_ComputerSystem Where Description = 'Microsoft Virtual Machine'") 'マシンステータスが2(起動中)だった場合にスナップショットを取得 For Each VM In VMList If VM.EnabledState = 2 Then WriteLog VM.ElementName CreateSnapshot(VM.ElementName) Else WriteLog Format1("{0} は起動していません。" , VM.ElementName) End If Next End Sub '----------------------------------------------------------------- ' CreateSnapshot '----------------------------------------------------------------- Sub CreateSnapshot(vmName) dim computer, vm computer = "." set objWMIService = GetObject("winmgmts:\\" & computer & "\root\virtualization") set managementService = objWMIService.ExecQuery("select * from Msvm_VirtualSystemManagementService").ItemIndex(0) set vm = GetComputerSystem(vmName) if CreateVirtualSystemSnapshot(vm) then WriteLog "Done" else WriteLog "CreateVirtualSystemSnapshot Failed." end if End Sub '----------------------------------------------------------------- ' Retrieve Msvm_VirtualComputerSystem from base on its ElementName '----------------------------------------------------------------- Function GetComputerSystem(vmElementName) On Error Resume Next dim query query = Format1("select * from Msvm_ComputerSystem where ElementName = '{0}'", vmElementName) set GetComputerSystem = objWMIService.ExecQuery(query).ItemIndex(0) if (Err.Number <> 0) then WriteLog Format1("Err.Number: {0}", Err.Number) WriteLog Format1("Err.Description:{0}", Err.Description) WScript.Quit(1) end if End Function '----------------------------------------------------------------- ' Create a virtual system snapshot '----------------------------------------------------------------- Function CreateVirtualSystemSnapshot(vm) dim objInParam, objOutParams CreateVirtualSystemSnapshot = false set objInParam = managementService.Methods_("CreateVirtualSystemSnapshot").InParameters.SpawnInstance_() objInParam.SourceSystem = vm.Path_.Path set objOutParams = managementService.ExecMethod_("CreateVirtualSystemSnapshot", objInParam) if (objOutParams.ReturnValue = wmiStarted) then if (WMIJobCompleted(objOutParams)) then CreateVirtualSystemSnapshot = true end if elseif (objOutParams.ReturnValue = wmiSuccessful) then CreateVirtualSystemSnapshot = true else WriteLog Format1("CreateVirtualSystemSnapshot failed with ReturnValue {0}", objOutParams.ReturnValue) end if End Function '----------------------------------------------------------------- ' Handle wmi Job object '----------------------------------------------------------------- Function WMIJobCompleted(outParam) WriteLog "Function WMIJobCompleted" dim WMIJob, jobState set WMIJob = objWMIService.Get(outParam.Job) WMIJobCompleted = true jobState = WMIJob.JobState while jobState = JobRunning or jobState = JobStarting WriteLog Format1("In progress... {0}% completed.",WMIJob.PercentComplete) WScript.Sleep(1000) set WMIJob = objWMIService.Get(outParam.Job) jobState = WMIJob.JobState wend if (jobState <> JobCompleted) then WriteLog Format1("ErrorCode:{0}", WMIJob.ErrorCode) WriteLog Format1("ErrorDescription:{0}", WMIJob.ErrorDescription) WMIJobCompleted = false end if End Function '----------------------------------------------------------------- ' Create the console log files. '----------------------------------------------------------------- Sub WriteLog(line) dim fileStream set fileStream = fileSystem.OpenTextFile(".\CreateVirtualSystemSnapshot.log", 8, true) WScript.Echo line fileStream.WriteLine line fileStream.Close End Sub '------------------------------------------------------------------------------ ' The string formating functions to avoid string concatenation. '------------------------------------------------------------------------------ Function Format2(myString, arg0, arg1) Format2 = Format1(myString, arg0) Format2 = Replace(Format2, "{1}", arg1) End Function '------------------------------------------------------------------------------ ' The string formating functions to avoid string concatenation. '------------------------------------------------------------------------------ Function Format1(myString, arg0) Format1 = Replace(myString, "{0}", arg0) End Function
とりあえず定期的にスナップショットを取得出来れば万が一の場合にもきっと役に立つと思います。