WIN1@Codename

Codename

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

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

コメント

コメントの投稿


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

トラックバック

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

FC2Ad