WIN1@Codename

Codename

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

仮想マシンの設定で、CPUの数を変更することができます。
スクリプトで書いてみると以下のようになります。

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 SetProcessor(strVMName, 2)

Function SetProcessor(strVMName, intProcessor)
    Dim objComputerSystem
    Set objComputerSystem = objWMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName = '" & strVMName & "'").ItemIndex(0)

    Dim objVirtualSystemsettingData
    Set objVirtualSystemsettingData = objWMIService.ExecQuery("ASSOCIATORS OF {" & objComputerSystem.Path_.Path & "} WHERE ResultClass = Msvm_VirtualSystemsettingData").ItemIndex(0)

    Dim objProcessorSettingData
    Set objProcessorSettingData = objWMIService.ExecQuery("ASSOCIATORS OF {" & objVirtualSystemsettingData.Path_.Path & "} WHERE ResultClass = Msvm_ProcessorSettingData").ItemIndex(0)
    objProcessorSettingData.VirtualQuantity = intProcessor

    Dim objVirtualSystemManagementService
    Set objVirtualSystemManagementService = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemManagementService").ItemIndex(0)

    Dim strResourceSettingData
    strResourceSettingData = Array(1)
    strResourceSettingData(0) = objProcessorSettingData.GetText_(1)  

    Dim objInParam, objOutParams
    Set objInParam = objManagementService.Methods_("ModifyVirtualSystemResources").InParameters.SpawnInstance_()
    objInParam.ResourceSettingData = strResourceSettingData
    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"
        Wscript.Echo objOutParams.ReturnValue
    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 

この例ではCPUの数を2つに増やしています。

コメント

コメントの投稿


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

トラックバック

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

FC2Ad