WIN1@Codename

Codename

スポンサーサイト

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

Hyper-V をプログラムでどうにかしてみる VBScript その12 (DVDドライブの追加)

今回は仮想マシンにDVDドライブを追加して、isoファイルをマウントしてみましょう。
手順としては、IDEコントローラーにDVDドライブを追加して、isoイメージをマウントするといった次第です。

Option Explicit

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

Call SetDvdDrive(strVMName)
Call SetIso(strVMName, strIsoPath)

Function SetIso(strVMName, strIsoPath)
    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 CD/DVD 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 objIso
    If objSettingsDefineCapabilities.ValueRole = 0 Then
        Set objIso = objWMIService.Get(objSettingsDefineCapabilities.PartComponent)
    End If
  
    Dim objVirtualSystemSettingData
    Set objVirtualSystemSettingData = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemSettingData WHERE ElementName = '" & strVMName & "'").ItemIndex(0)
   
    Dim objIDEController, objResourceAllocationSettingData
    Set objResourceAllocationSettingData = objWMIService.ExecQuery("ASSOCIATORS OF {Msvm_VirtualSystemSettingData='" & objVirtualSystemSettingData.InstanceID & "'} Where ResultClass=Msvm_ResourceAllocationSettingData AssocClass=Msvm_VirtualSystemSettingDataComponent")
    For Each objIDEController In objResourceAllocationSettingData   
        If objIDEController.ResourceSubType = "Microsoft Emulated IDE Controller" Then
Exit For
        End If
    Next
   
    Dim objDvdDrive, strIsoPaths
    strIsoPaths = Array(1)
    strIsoPaths(0) = strIsoPath
    Set objDvdDrive = objWMIService.ExecQuery("SELECT * FROM Msvm_ResourceAllocationSettingData WHERE Parent = '" & Replace(objIDEController.Path_.Path, "\", "\\") & "' AND Address = '1'").ItemIndex(0)
    objIso.Parent = objDvdDrive.Path_.Path
    objIso.Connection = strIsoPaths

    Dim strResourceSettingData
    strResourceSettingData = Array(1)
    strResourceSettingData(0) = objIso.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 SetDvdDrive(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=16 and ResourceSubType='Microsoft Synthetic DVD 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 objDvdDrive
    If objSettingsDefineCapabilities.ValueRole = 0 Then
        Set objDvdDrive = 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 = '1'").ItemIndex(0)
    objDvdDrive.Parent = objIDEController.Path_.Path
    objDvdDrive.Address = 0

    Dim strResourceSettingData
    strResourceSettingData = Array(1)
    strResourceSettingData(0) = objDvdDrive.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

2ステップとなりますが、こんな感じとなります。

コメント

コメントの投稿


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

トラックバック

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

FC2Ad

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