スクリプトでHyper-V上の仮想OSのプロパティをVBScriptで取得する方法

001.png
ネット上で参考になるHyper-Vのスクリプトを見て、以前Virtual Server上で実行していたVBScriptを多少書き換えればHyper-Vでも稼働させる事が出来そうだなと思ってHyper-Vのインストールされた環境で動作するVBScriptを作ってみました。

とりあえず全ての仮想インスタンスのステータスを表示します。

“Msvm_ComputerSystem”をそのまま利用すると物理OSのステータスも拾ってしまうので

Where Description = 'Microsoft Virtual Machine'

というところで物理OSをリストから除外しています。

とりあえず一通りのプロパティの取得はWMIから出来ますので、Virtual Serverと同じ様に自動起動や自動バックアップがVBScriptで作れそうです。

Option Explicit

Dim objWMI
Dim VM
Dim VMList

Set objWMI = GetObject("winmgmts:\\.\root\virtualization")
'日本語環境ではホストマシンのDescriptionが'Microsoft ホスト コンピュータ システム'となるのでそれをクエリで除外
Set VMList = objWMI.ExecQuery _
	("SELECT * FROM Msvm_ComputerSystem Where Description = 'Microsoft Virtual Machine'")

'マシン名と詳細の表示
For Each VM In VMList
	Wscript.Stdout.WriteLine "ElementName                   " & VM.ElementName
	Wscript.Stdout.WriteLine "Name                          " & VM.Name
	Wscript.Stdout.WriteLine "EnabledState                  " & SHOW_EnabledState(VM.EnabledState)
	Wscript.Stdout.WriteLine "Description                   " & VM.Description
	Wscript.Stdout.WriteLine "InstallDate                   " & VM.InstallDate
	Wscript.Stdout.WriteLine "TimeOfLastConfigurationChange " & VM.TimeOfLastConfigurationChange
	Wscript.Stdout.WriteLine "TimeOfLastStateChange         " & VM.TimeOfLastStateChange
	Wscript.Stdout.WriteBlankLines 1
Next

'EnabledStateのintを文字に変換
Function SHOW_EnabledState(int)
	Select Case int
		Case 0
			SHOW_EnabledState = "Unknown"
		Case 2
			SHOW_EnabledState = "Enabled(実行中)"
		Case 3
			SHOW_EnabledState = "Disabled"
		Case 32768
			SHOW_EnabledState = "Paused(一時停止)"
		Case 32769
			SHOW_EnabledState = "Suspended(保存完了)"
		Case 32770
			SHOW_EnabledState = "Starting(復元中)"
		Case 32771
			SHOW_EnabledState = "Snapshotting"
		Case 32772
			SHOW_EnabledState = "Migrating"
		Case 32773
			SHOW_EnabledState = "Saving"
		Case 32774
			SHOW_EnabledState = "Stopping"
		Case 32775
			SHOW_EnabledState = "Deleted"
		Case 32776
			SHOW_EnabledState = "Pausing"
		Case Else
			SHOW_EnabledState = "不明なステータス " & int
	End Select
End Function

参考URL

Msvm_ComputerSystem Class
Hyper-V シャットダウンスクリプト

Msvm_ComputerSystem instanceの一覧

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\virtualization") 
Set colItems = objWMIService.ExecQuery( _
	"SELECT * FROM Msvm_ComputerSystem WHERE Description = 'Microsoft Virtual Machine'",,48) 
For Each objItem in colItems 
	Wscript.Echo "-----------------------------------"
	Wscript.Echo "Msvm_ComputerSystem instance"
	Wscript.Echo "-----------------------------------"
	If isNull(objItem.AssignedNumaNodeList) Then
		Wscript.Echo "AssignedNumaNodeList: "
	Else
		Wscript.Echo "AssignedNumaNodeList: " & Join(objItem.AssignedNumaNodeList, ",")
	End If
	Wscript.Echo "Caption: " & objItem.Caption
	Wscript.Echo "CreationClassName: " & objItem.CreationClassName
	If isNull(objItem.Dedicated) Then
		Wscript.Echo "Dedicated: "
	Else
		Wscript.Echo "Dedicated: " & Join(objItem.Dedicated, ",")
	End If
	Wscript.Echo "Description: " & objItem.Description
	Wscript.Echo "ElementName: " & objItem.ElementName
	Wscript.Echo "EnabledDefault: " & objItem.EnabledDefault
	Wscript.Echo "EnabledState: " & objItem.EnabledState
	Wscript.Echo "HealthState: " & objItem.HealthState
	If isNull(objItem.IdentifyingDescriptions) Then
		Wscript.Echo "IdentifyingDescriptions: "
	Else
		Wscript.Echo "IdentifyingDescriptions: " & Join(objItem.IdentifyingDescriptions, ",")
	End If
	Wscript.Echo "InstallDate: " & objItem.InstallDate
	Wscript.Echo "Name: " & objItem.Name
	Wscript.Echo "NameFormat: " & objItem.NameFormat
	Wscript.Echo "OnTimeInMilliseconds: " & objItem.OnTimeInMilliseconds
	If isNull(objItem.OperationalStatus) Then
		Wscript.Echo "OperationalStatus: "
	Else
		Wscript.Echo "OperationalStatus: " & Join(objItem.OperationalStatus, ",")
	End If
	If isNull(objItem.OtherDedicatedDescriptions) Then
		Wscript.Echo "OtherDedicatedDescriptions: "
	Else
		Wscript.Echo "OtherDedicatedDescriptions: " & Join(objItem.OtherDedicatedDescriptions, ",")
	End If
	Wscript.Echo "OtherEnabledState: " & objItem.OtherEnabledState
	If isNull(objItem.OtherIdentifyingInfo) Then
		Wscript.Echo "OtherIdentifyingInfo: "
	Else
		Wscript.Echo "OtherIdentifyingInfo: " & Join(objItem.OtherIdentifyingInfo, ",")
	End If
	If isNull(objItem.PowerManagementCapabilities) Then
		Wscript.Echo "PowerManagementCapabilities: "
	Else
		Wscript.Echo "PowerManagementCapabilities: " & Join(objItem.PowerManagementCapabilities, ",")
	End If
	Wscript.Echo "PrimaryOwnerContact: " & objItem.PrimaryOwnerContact
	Wscript.Echo "PrimaryOwnerName: " & objItem.PrimaryOwnerName
	Wscript.Echo "ProcessID: " & objItem.ProcessID
	Wscript.Echo "RequestedState: " & objItem.RequestedState
	Wscript.Echo "ResetCapability: " & objItem.ResetCapability
	If isNull(objItem.Roles) Then
		Wscript.Echo "Roles: "
	Else
		Wscript.Echo "Roles: " & Join(objItem.Roles, ",")
	End If
	Wscript.Echo "Status: " & objItem.Status
	If isNull(objItem.StatusDescriptions) Then
		Wscript.Echo "StatusDescriptions: "
	Else
		Wscript.Echo "StatusDescriptions: " & Join(objItem.StatusDescriptions, ",")
	End If
	Wscript.Echo "TimeOfLastConfigurationChange: " & objItem.TimeOfLastConfigurationChange
	Wscript.Echo "TimeOfLastStateChange: " & objItem.TimeOfLastStateChange
Next

1 Comment

スクリプトでHyper-V上の仮想OSのプロパティをVBScriptで取得する方法 … プラウザでプレビュー

[…] See original here: スクリプトでHyper-V上の仮想OSのプロパティをVBScriptで取得する方法 … By admin | category: プロパティ | tags: japanese, join-their, option, property-manager, プロパティ | voglia.jp […]

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です