WIN1@Codename

Codename

スポンサーサイト

上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

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つに増やしています。

スポンサーサイト

Hyper-V をプログラムでどうにかしてみる VBScript その8 (仮想マシン一覧の取得)

Hyper-Vマネージャーに登録されている仮想マシンのリスト一覧を取得してみます。
Option Explicit 

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

Call GetVMList()
 
Function GetVMList()
    Dim objComputerSystems, objComputerSystem, strList
    Set objComputerSystems = objWMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem WHERE Caption LIKE '仮想マシン'")
    Dim intState, strState 
    For Each objComputerSystem In objComputerSystems       
        intState = objComputerSystem.EnabledState       
        Select Case intState
            Case 0
                strState = "Unknown"
            Case 2
                strState = "Enabled(実行中)"
            Case 3
                strState = "Disabled(オフ)"
            Case 32768
                strState = "Paused(一時停止)"
            Case 32769
                strState = "Suspended(保存完了)"
            Case 32770
                strState = "Starting(復元中)"
            Case 32771
                strState = "Snapshotting"
            Case 32772
                strState = "Migrating"
            Case 32773
                strState = "Saving"
            Case 32774
                strState = "Stopping"
            Case 32775
                strState = "Deleted"
            Case 32776
                strState = "Pausing"
            Case Else
                strState = "不明なステータス " & intState
        End Select
        strList = strList & vbCrLf & objComputerSystem.ElementName & " "  & strState
    Next   
    Wscript.Echo strList
End Function

Hyper-V をプログラムでどうにかしてみる VBScript その7 (仮想マシンのシャットダウン)

前回のステータス変更で抜けていたのが、仮想マシンの「シャットダウン」です。
こちらは、別のスクリプトで実施することになります。
事前に統合サービスのインストールが仮想マシン側に必要なようですので、ご注意ください。

Option Explicit

Dim strServerName, strVMName, strNetworkName, strDiskPath, objWMIService, objManagementService
strServerName = "."
strVMName = "TestVM"  
strNetworkName = "LAN - 仮想ネットワーク"
strDiskPath = "E:\Hyper-V\TestVM\TestVM.vhd"
Set objWMIService = GetObject("winmgmts:\\" & strServerName & "\root\virtualization")
Set objManagementService = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemManagementService").ItemIndex(0)

Call VMShutDown(strVMName)

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

    Dim objShutdownComponent
    Set objShutdownComponent = objWMIService.ExecQuery("SELECT * FROM Msvm_ShutdownComponent WHERE SystemName='" & objComputerSystem.Name & "'").ItemIndex(0)

    Dim objInParam, objOutParams
    Set objInParam = objShutdownComponent.Methods_("InitiateShutdown").InParameters.SpawnInstance_()
    objInParam.Force = True
    objInParam.Reason = ""
    Set objOutParams = objShutdownComponent.ExecMethod_("InitiateShutdown", 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

Hyper-V をプログラムでどうにかしてみる VBScript その6 (仮想マシンの状態変更)

これまでのスクリプトで作成した仮想ましんを今度は起動してみましょう。
仮想マシンの「起動(2)・停止(3)・リブート(10)・・リセット(11)・一時停止(32768)・保存(32769)」 が同様のスクリプトで可能です。

Option
Explicit

Dim strServerName, strVMName, strNetworkName, strDiskPath, objWMIService, objManagementService
strServerName = "."
strVMName = "TestVM"  
strNetworkName = "LAN - 仮想ネットワーク"
strDiskPath = "E:\Hyper-V\TestVM\TestVM.vhd"
Set objWMIService = GetObject("winmgmts:\\" & strServerName & "\root\virtualization")
Set objManagementService = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemManagementService").ItemIndex(0)

Call VMStatusChange(strVMName, 2)

Function VMStatusChange(strVMName, intStatus)
    'Start=2 Disabled=3 Reboot=10 Reset=11 Paused=32768 Suspended=32769
    Dim objComputerSystem
    Set objComputerSystem = objWMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName = '" & strVMName & "'").ItemIndex(0)

    Dim objInParam, objOutParams
    Set objInParam = objComputerSystem.Methods_("RequestStateChange").InParameters.SpawnInstance_()
    objInParam.RequestedState = intStatus
    Set objOutParams = objComputerSystem.ExecMethod_("RequestStateChange", 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

Hyper-V をプログラムでどうにかしてみる VBScript その5 (仮想マシンの削除)

作った仮想マシンを削除するには

Option Explicit

Dim strServerName, strVMName, strNetworkName, strDiskPath, objWMIService, objManagementService
strServerName = "."
strVMName = "TestVM"  
strNetworkName = "LAN - 仮想ネットワーク"
strDiskPath = "E:\Hyper-V\TestVM\TestVM.vhd"
Set objWMIService = GetObject("winmgmts:\\" & strServerName & "\root\virtualization")
Set objManagementService = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemManagementService").ItemIndex(0)

Call DeleteVM(strVMName)

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

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

    Dim objInParam, objOutParams
    Set objInParam = objManagementService.Methods_("DestroyVirtualSystem").InParameters.SpawnInstance_()
    objInParam.ComputerSystem = objComputerSystem.Path_.Path
    Set objOutParams = objManagementService.ExecMethod_("DestroyVirtualSystem", 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

と、なりますね。

Hyper-V をプログラムでどうにかしてみる VBScript その4 (ハードディスクの設定)

今回はハードディスクの設定となります。
VB.NETでは一つのFunctionにまとめてしまいましたが、VBScriptではハードディスクドライブの追加と、ハードディスクの追加を分けて記述し、この二つを実行することでハードディスクの追加としています。

Option Explicit

Dim strServerName, strVMName, strNetworkName, strDiskPath, objWMIService, objManagementService
strServerName = "."
strVMName = "TestVM"  
strNetworkName = "LAN - 仮想ネットワーク"
strDiskPath = "E:\Hyper-V\TestVM\TestVM.vhd"
Set objWMIService = GetObject("winmgmts:\\" & strServerName & "\root\virtualization")
Set objManagementService = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemManagementService").ItemIndex(0)

Call SetDiskDrive(strVMName)
Call SetHardDisk(strVMName, strDiskPath)

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

    Dim objVirtualSwitchManagementService
    Set objVirtualSwitchManagementService = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSwitchManagementService").ItemIndex(0)

    Dim objResourcePool
    Set objResourcePool = objWMIService.ExecQuery("SELECT * FROM Msvm_ResourcePool WHERE ResourceType = 21 AND ResourceSubType = 'Microsoft Virtual Hard Disk' AND OtherResourceType = null").ItemIndex(0)

    Dim objAllocationCapabilities
    Set objAllocationCapabilities = objWMIService.ExecQuery("ASSOCIATORS OF {" & objResourcePool.Path_.Path & "} WHERE ResultClass = Msvm_AllocationCapabilities").ItemIndex(0)
   
    Dim objSettingsDefineCapabilities
    Set objSettingsDefineCapabilities = objWMIService.ExecQuery("REFERENCES OF {" & objAllocationCapabilities.Path_.Path & "} WHERE ResultClass = Msvm_SettingsDefineCapabilities").ItemIndex(0)
  
    Dim objHardDisk
    If objSettingsDefineCapabilities.ValueRole = 0 Then
        Set objHardDisk = objWMIService.Get(objSettingsDefineCapabilities.PartComponent)
    End If

    Dim objIDEController
    Set objIDEController = objWMIService.ExecQuery("SELECT * FROM Msvm_ResourceAllocationSettingData WHERE ResourceType = 5 AND ResourceSubType = 'Microsoft Emulated IDE Controller' AND Address = '0'").ItemIndex(0)
  
    Dim objDiskDrive, strDiskPaths
    strDiskPaths = Array(1)
    strDiskPaths(0) = strDiskPath
    Set objDiskDrive = objWMIService.ExecQuery("SELECT * FROM Msvm_ResourceAllocationSettingData WHERE Parent = '" & Replace(objIDEController.Path_.Path, "\", "\\") & "' AND Address = '0'").ItemIndex(0)
    objHardDisk.Parent = objDiskDrive.Path_.Path
    objHardDisk.Connection = strDiskPaths

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

    Dim objInParam, objOutParams
    Set objInParam = objManagementService.Methods_("AddVirtualSystemResources").InParameters.SpawnInstance_()
    objInParam.ResourceSettingData = strResourceSettingData
    objInParam.TargetSystem = objComputerSystem.Path_.Path
    Set objOutParams = objManagementService.ExecMethod_("AddVirtualSystemResources", objInParam)

    Call JobCompleted(objOutParams, objWMIService)
End Function

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

    Dim objResourcePool
    Set objResourcePool = objWMIService.ExecQuery("SELECT * FROM Msvm_ResourcePool WHERE ResourceType = 22 AND ResourceSubType = 'Microsoft Synthetic Disk Drive' AND OtherResourceType = null").ItemIndex(0)

    Dim objAllocationCapabilities
    Set objAllocationCapabilities = objWMIService.ExecQuery("ASSOCIATORS OF {" & objResourcePool.Path_.Path & "} WHERE ResultClass = Msvm_AllocationCapabilities").ItemIndex(0)
   
    Dim objSettingsDefineCapabilities
    Set objSettingsDefineCapabilities = objWMIService.ExecQuery("REFERENCES OF {" & objAllocationCapabilities.Path_.Path & "} WHERE ResultClass = Msvm_SettingsDefineCapabilities").ItemIndex(0)
  
    Dim objDiskDrive
    If objSettingsDefineCapabilities.ValueRole = 0 Then
        Set objDiskDrive = objWMIService.Get(objSettingsDefineCapabilities.PartComponent)
    End If

    Dim objIDEController
    Set objIDEController = objWMIService.ExecQuery("SELECT * FROM Msvm_ResourceAllocationSettingData WHERE ResourceType = 5 AND ResourceSubType = 'Microsoft Emulated IDE Controller' AND Address = '0'").ItemIndex(0)
    objDiskDrive.Parent = objIDEController.Path_.Path
    objDiskDrive.Address = 0

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

    Dim objInParam, objOutParams
    Set objInParam = objManagementService.Methods_("AddVirtualSystemResources").InParameters.SpawnInstance_()
    objInParam.ResourceSettingData = strResourceSettingData
    objInParam.TargetSystem = objComputerSystem.Path_.Path
    Set objOutParams = objManagementService.ExecMethod_("AddVirtualSystemResources", 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

と、ここまで一気にきましたが、一応これで仮想マシンが動くところまで設定ができたことになります。

Hyper-V をプログラムでどうにかしてみる VBScript その3 (ネットワークの設定)

続きまして、ネットワークの設定となります。
VBScriptもVB.NET同様、設定項目によりコードが徐々に複雑になってきますね。

Option Explicit

Dim strServerName, strVMName,  strNetworkName, objWMIService, objManagementService
strServerName = "."
strVMName = "TestVM"  
strNetworkName = "LAN - 仮想ネットワーク"
Set objWMIService = GetObject("winmgmts:\\" & strServerName & "\root\virtualization")
Set objManagementService = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemManagementService").ItemIndex(0)

Call SetNetworkAdapter(strVMName, strNetworkName)

Function SetNetworkAdapter(strVMName, strNetworkName)
    Dim objComputerSystem
    Set objComputerSystem = objWMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName = '" & strVMName & "'").ItemIndex(0)
   
    Dim objVirtualSwitchManagementService
    Set objVirtualSwitchManagementService = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSwitchManagementService").ItemIndex(0)
   
    Dim objVirtualSwitch
    Set objVirtualSwitch = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSwitch WHERE ElementName = '" & strNetworkName & "'").ItemIndex(0)
   
    Dim objInParam, objOutParams
    Set objInParam = objVirtualSwitchManagementService.Methods_("CreateSwitchPort").InParameters.SpawnInstance_()
    objInParam.Name = Replace(Replace(Replace(FormatDateTime(Now, 0), "/", ""), ":", ""), " ", "")
    objInParam.FriendlyName = strNetworkName
    objInParam.VirtualSwitch = objVirtualSwitch.Path_.Path
    Set objOutParams = objVirtualSwitchManagementService.ExecMethod_("CreateSwitchPort", objInParam)

    Dim objSwitchPort
    Set objSwitchPort = objWMIService.Get(objOutParams.CreatedSwitchPort)

    Dim objEmulatedEthernetPortSettingData
    Set objEmulatedEthernetPortSettingData = objWMIService.ExecQuery("SELECT * FROM Msvm_SyntheticEthernetPortSettingData WHERE InstanceID like '%Default%'").ItemIndex(0)
   
    Dim objResourcePool
    Set objResourcePool = objWMIService.ExecQuery("SELECT * FROM Msvm_ResourcePool WHERE ResourceType = 10 AND ResourceSubType = 'Microsoft Synthetic Ethernet Port' AND OtherResourceType = null").ItemIndex(0)
   
    Dim objAllocationCapabilities
    Set objAllocationCapabilities = objWMIService.ExecQuery("ASSOCIATORS OF {" & objResourcePool.Path_.Path & "} WHERE ResultClass = Msvm_AllocationCapabilities").ItemIndex(0)
  
    Dim objSettingsDefineCapabilities
    Set objSettingsDefineCapabilities = objWMIService.ExecQuery("REFERENCES OF {" & objAllocationCapabilities.Path_.Path & "} WHERE ResultClass = Msvm_SettingsDefineCapabilities").ItemIndex(0)
   
    Dim objDefaultNic
    If objSettingsDefineCapabilities.ValueRole = 0 Then
        Set objDefaultNic = objWMIService.Get(objSettingsDefineCapabilities.PartComponent)
    End If

    Dim objVirtualSystemManagementService
    Set objVirtualSystemManagementService = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemManagementService").ItemIndex(0)
    
    Dim strConnection
    strConnection = Array(1)
    strConnection(0) = objSwitchPort.Path_.Path
    objDefaultNic.Connection = strConnection
    objDefaultNic.StaticMacAddress = False
    objDefaultNic.ElementName = "ネットワーク アダプター"

    Dim objTypeLib
    Set objTypeLib = CreateObject("Scriptlet.TypeLib")
    Dim strVirtualSystemIdentifiers
    strVirtualSystemIdentifiers = Array(1)

    strVirtualSystemIdentifiers(0) =  objTypeLib.Guid
    objDefaultNic.VirtualSystemIdentifiers = strVirtualSystemIdentifiers
      
    Dim strResourceSettingData
    strResourceSettingData = Array(1)
    strResourceSettingData(0) = objDefaultNic.GetText_(1)  

    Dim objInParam2, objOutParams2
    Set objInParam2 = objManagementService.Methods_("AddVirtualSystemResources").InParameters.SpawnInstance_()
    objInParam2.ResourceSettingData = strResourceSettingData
    objInParam2.TargetSystem = objComputerSystem.Path_.Path
    Set objOutParams2 = objManagementService.ExecMethod_("AddVirtualSystemResources", objInParam2)

    Call JobCompleted(objOutParams2, 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」という仮想マシンに「LAN - 仮想ネットワーク」と名づけた仮想ネットワーク設定を接続しています。
「LAN」仮想ネットワーク設定は事前にHyper-Vマネージャーの仮想ネットワークマネージャーより作成してあることとします。

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のメモリを割り当てています。

Hyper-V をプログラムでどうにかしてみる VBScript その1 (仮想マシンの作成)

PowerShellでのHyper-Vの管理スクリプトはネット上にたくさんあるのですが、VBS(WSH)のサンプルとなると、なかなかみつかりません。

そこで、以前VB.NETで作成した内容を基にVBSでHyper-Vをどうにかしてみるスクリプトを参考までにいくつかアップしてみようかと思います。

まずは、基本となる仮想マシンの作成です。
今回は仮想マシン名を "TestVM" として、"E:\Hyper-V\TestVM" に仮想マシンを作成しています。


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 DefineVM(strVMName)

Function DefineVM(strVMName) 
    Dim objVirtualSystemGlobalsettingData
    Set objVirtualSystemGlobalsettingData = objWMIService.Get("Msvm_VirtualSystemGlobalsettingData").SpawnInstance_()
    objVirtualSystemGlobalsettingData.ElementName = strVMName
    objVirtualSystemGlobalsettingData.ExternalDataRoot = "E:\Hyper-V\" & strVMName

    Dim objInParam, objOutParams
    Set objInParam = objManagementService.Methods_("DefineVirtualSystem").InParameters.SpawnInstance_()
    objInParam.ResourcesettingData = Null
    objInParam.Sourcesetting = Null
    objInParam.SystemsettingData = objVirtualSystemGlobalsettingData.GetText_(1)
    Set objOutParams = objManagementService.ExecMethod_("DefineVirtualSystem", 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


と、ここまではよくあるサンプルですね。
さらにつづきます。

FC2Ad

上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。