Hyper-Vの仮想HDDのバックアップ時やもろもろに必要だったのでこちらを参考に修正。
指定した仮想PCの起動
Dim strArg ' 引数情報
Dim objWMI, VMList, VM
Set objWMI = GetObject("winmgmts:\.rootvirtualization")
Set VMList = objWMI.ExecQuery("SELECT * FROM Msvm_ComputerSystem")
For Each strArg In WScript.Arguments
For Each VM In VMList
If VM.Caption = "仮想マシン" then
If VM.ElementName = strArg Then
WScript.Echo strArg & "を起動中。"
VM.RequestStateChange(2)
End If
End if
Next
Next
指定した仮想PCの停止
Dim strArg ' 引数情報
Dim objWMI, VMList, VM, objVMShutdown, objRes
Set objWMI = GetObject("winmgmts:\.rootvirtualization")
Set VMList = objWMI.ExecQuery("SELECT * FROM Msvm_ComputerSystem")
For Each strArg In WScript.Arguments
For Each VM In VMList
If VM.Caption = "仮想マシン" and VM.EnabledState = 2 then
If VM.ElementName = strArg Then
If Not VM.EnabledState = 32769 Then
Set objVMShutdown = objWMI.ExecQuery("SELECT * FROM Msvm_ShutdownComponent WHERE SystemName='" & VM.Name & "'")
objRes = objVMShutdown.ItemIndex(0).InitiateShutdown(True,"Scripted Shutdown")
WScript.Echo strArg & "を停止中。"
Else
Msgbox "NG"
End If
End if
End if
Next
Next
Comments