WIN1@Codename

Codename

Hyper-V をプログラムでどうにかしてみる VBScript その11 (SCSIディスクの追加)

前回、SCSIコントローラーを追加しましたので、今度はSCSIハードディスクを追加してみます。
少々手順が複雑なのですが、まずはディスクドライブを追加した上で、vhdハードディスクを接続するといった手順になります。

Option Explicit

Dim strServerName, strVMName, strDiskPath, objWMIService, objManagementService
strServerName = "."
strVMName = "TestVM"  
strDiskPath = "E:\Hyper-V\TestVM\ScsiDisk.vhd"
Set objWMIService = GetObject("winmgmts:\\" & strServerName & "\root\virtualization")
Set objManagementService = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemManagementService").ItemIndex(0)

Call SetScsiDiskDrive(strVMName)
Call SetScsiHardDisk(strVMName, strDiskPath)

Function SetSCSIHardDisk(strVMName, strDiskPath)
    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 = 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 objVirtualSystemSettingData
    Set objVirtualSystemSettingData = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemSettingData WHERE ElementName = '" & strVMName & "'").ItemIndex(0)

    Dim objSCSIController, objResourceAllocationSettingData
    Set objResourceAllocationSettingData = objWMIService.ExecQuery("Associators of {Msvm_VirtualSystemSettingData='" & objVirtualSystemSettingData.InstanceID & "'} Where ResultClass=Msvm_ResourceAllocationSettingData AssocClass=Msvm_VirtualSystemSettingDataComponent")
    For Each objSCSIController In objResourceAllocationSettingData   
        If objSCSIController.ResourceSubType = "Microsoft Synthetic SCSI Controller" Then
Exit For
        End If
    Next

    Dim objDiskDrive, strDiskPaths
    strDiskPaths = Array(1)
    strDiskPaths(0) = strDiskPath
    Set objDiskDrive = objWMIService.ExecQuery("SELECT * FROM Msvm_ResourceAllocationSettingData WHERE Parent = '" & Replace(objSCSIController.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 SetSCSIDiskDrive(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 objVirtualSystemSettingData
    Set objVirtualSystemSettingData = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemSettingData WHERE ElementName = '" & strVMName & "'").ItemIndex(0)
   
    Dim objSCSIController, objResourceAllocationSettingData
    Set objResourceAllocationSettingData = objWMIService.ExecQuery("Associators of {Msvm_VirtualSystemSettingData='" & objVirtualSystemSettingData.InstanceID & "'} Where ResultClass=Msvm_ResourceAllocationSettingData AssocClass=Msvm_VirtualSystemSettingDataComponent")
    For Each objSCSIController In objResourceAllocationSettingData   
        If objSCSIController.ResourceSubType = "Microsoft Synthetic SCSI Controller" Then
Exit For
        End If
    Next
 
    objDiskDrive.Parent = objSCSIController.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"
        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

長いです。。。
スクリプトでこの長さはいやになってしまいますね。
ここまでになると、VB.netに切り替えたほうがよかなと思います。

コメント

コメントの投稿


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

トラックバック

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

FC2Ad