WIN1@Codename

Codename

Hyper-V をプログラムでどうにかしてみる VBScript その2 (メモリの設定)

今回は、メモリの搭載値を任意に設定するスクリプトとなります。

Option Explicit

Dim strServerName, strVMName,  objWMIService, objManagementService
strServerName = "."
strVMName = "TestVM"  
Set objWMIService = GetObject("winmgmts:\\" & strServerName & "\root\virtualization")
Set objManagementService = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemManagementService").ItemIndex(0)

Call SetMemory(strVMName, 1024)

Function SetMemory(strVMName, intMemory)     
    Dim objComputerSystem
    Set objComputerSystem = objWMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName = '" & strVMName & "'").ItemIndex(0)
    Dim objVirtualSystemSettingData
    Set objVirtualSystemSettingData = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemSettingData WHERE ElementName = '" & strVMName & "'").ItemIndex(0)
    Dim objMemorySettingData
    Set objMemorySettingData = objWMIService.ExecQuery("ASSOCIATORS OF {" & objVirtualSystemSettingData.Path_.Path & "} WHERE ResultClass = Msvm_MemorySettingData").ItemIndex(0)
    objMemorySettingData.VirtualQuantity = intMemory
    Dim strMemorySettingData
    strMemorySettingData = Array(1)
    strMemorySettingData(0) = objMemorySettingData.GetText_(1)

    Dim objInParam, objOutParams
    Set objInParam = objManagementService.Methods_("ModifyVirtualSystemResources").InParameters.SpawnInstance_()
    objInParam.ResourceSettingData = strMemorySettingData
    objInParam.ComputerSystem = objComputerSystem.Path_.Path
    Set objOutParams = objManagementService.ExecMethod_("ModifyVirtualSystemResources", objInParam)

    Call JobCompleted(objOutParams, objWMIService)
End Function

Function JobCompleted(objOutParams, objWMIService)
    Dim objJob
    If (objOutParams.ReturnValue = 0) Then
        Wscript.Echo "OK"
    ElseIf (objOutParams.ReturnValue <> 4096) Then
        Wscript.Echo "NG"
    Else  
        Set objJob = objWMIService.Get(objOutParams.Job)
        While (objJob.JobState = 3) Or (objJob.JobState = 4)
WScript.Sleep(1000)
Set objJob = objWMIService.Get(objOutParams.Job)
        Wend
        If (objJob.JobState <> 7) Then
Wscript.Echo "NG"
Wscript.Echo "ErrorCode:" & objJob.ErrorCode
Wscript.Echo "ErrorDescription:" & objJob.ErrorDescription
        Else
Wscript.Echo "OK"
        End If
    End If
End Function

前回作成した「TestVM」という仮想マシンに1024MBのメモリを割り当てています。

コメント

コメントの投稿


管理者にだけ表示を許可する

トラックバック

トラックバック URL
http://codename2010.blog11.fc2.com/tb.php/158-22fcc305
この記事にトラックバックする(FC2ブログユーザー)

FC2Ad