WIN1@Codename

Codename

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

TechNetの方でDVDのマウントを自動で行いたいとの質問があったので、このシリーズに追加してみました。

Dim objConnectionOptions As New ConnectionOptions()
objConnectionOptions.Impersonation = ImpersonationLevel.Impersonate
objConnectionOptions.EnablePrivileges = True
objConnectionOptions.Username = strAccount
objConnectionOptions.Password = strPassword
Dim objManagementScope As New ManagementScope("\\" + strServer + "\root\virtualization", objConnectionOptions)
objManagementScope.Connect()

Dim objComputerSystem As ManagementObject = Nothing
For Each objManagementObject As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName = 'TestVM'")).Get
    objComputerSystem = objManagementObject
Next

Dim objDiskDrive As ManagementObject = Nothing
For Each objResourcePool As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_ResourcePool WHERE ResourceType = 16 AND ResourceSubType = 'Microsoft Synthetic DVD Drive' AND OtherResourceType = null")).Get
    Dim objAllocationCapabilitiesCollection As ManagementObjectCollection = objResourcePool.GetRelated("Msvm_AllocationCapabilities")
    For Each objAllocationCapabilities As ManagementObject In objAllocationCapabilitiesCollection
Dim objSettingsDefineCapabilitiesCollection As ManagementObjectCollection = objAllocationCapabilities.GetRelationships("Msvm_SettingsDefineCapabilities")
For Each objSettingsDefineCapabilities As ManagementObject In objSettingsDefineCapabilitiesCollection
    If objSettingsDefineCapabilities("ValueRole") = 0 Then
objDiskDrive = New ManagementObject(objSettingsDefineCapabilities("PartComponent").ToString())
objDiskDrive.Scope = objManagementScope
    End If
Next
    Next
Next

Dim objIDEController As ManagementObject = Nothing
For Each objManagementObject As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_ResourceAllocationSettingData WHERE ResourceType = 5 AND ResourceSubType = 'Microsoft Emulated IDE Controller' AND Address = '1'")).Get
    objIDEController = objManagementObject
    objDiskDrive("Parent") = objIDEController.Path.Path
    objDiskDrive("Address") = 0
Next

For Each objVirtualSystemManagementService As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_VirtualSystemManagementService")).Get
    Dim objParams As ManagementBaseObject = objVirtualSystemManagementService.GetMethodParameters("AddVirtualSystemResources")
    Dim strResourcesettingData As String() = New String(0) {}
    strResourcesettingData(0) = objDiskDrive.GetText(TextFormat.CimDtd20)
    objParams("ResourceSettingData") = strResourcesettingData
    objParams("TargetSystem") = objComputerSystem.Path.Path
    Dim objManagementBaseObject As ManagementBaseObject = objVirtualSystemManagementService.InvokeMethod("AddVirtualSystemResources", objParams, Nothing)
Next

Dim objDVD As ManagementObject = Nothing
For Each objResourcePool As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_ResourcePool WHERE ResourceType = 21 AND ResourceSubType = 'Microsoft Virtual CD/DVD Disk' AND OtherResourceType = null")).Get
    Dim objAllocationCapabilitiesCollection As ManagementObjectCollection = objResourcePool.GetRelated("Msvm_AllocationCapabilities")
    For Each objAllocationCapabilities As ManagementObject In objAllocationCapabilitiesCollection
Dim objSettingsDefineCapabilitiesCollection As ManagementObjectCollection = objAllocationCapabilities.GetRelationships("Msvm_SettingsDefineCapabilities")
For Each objSettingsDefineCapabilities As ManagementObject In objSettingsDefineCapabilitiesCollection
    If objSettingsDefineCapabilities("ValueRole") = 0 Then
objDVD = New ManagementObject(objSettingsDefineCapabilities("PartComponent").ToString())
objDVD.Scope = objManagementScope
    End If
Next
    Next
Next

Dim objIDEDrive As ManagementObject = Nothing
For Each objManagementObject As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_ResourceAllocationSettingData WHERE Parent = '" & objIDEController.Path.Path.Replace("\", "\\") & "' AND Address = '0'")).Get
    objIDEDrive = objManagementObject
    objDVD("Parent") = objIDEDrive.Path.Path
    objDVD("Connection") = New String(0) {"C:\Hyper-V\TestVM.iso"}
Next

For Each objVirtualSystemManagementService As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_VirtualSystemManagementService")).Get
    Dim objParams As ManagementBaseObject = objVirtualSystemManagementService.GetMethodParameters("AddVirtualSystemResources")
    Dim strResourcesettingData As String() = New String(0) {}
    strResourcesettingData(0) = objDVD.GetText(TextFormat.CimDtd20)
    objParams("ResourcesettingData") = strResourcesettingData
    objParams("TargetSystem") = objComputerSystem.Path.Path
    Dim objManagementBaseObject As ManagementBaseObject = objVirtualSystemManagementService.InvokeMethod("AddVirtualSystemResources", objParams, Nothing)
    Console.WriteLine("SetHardDisk=" & objManagementBaseObject("ReturnValue"))
Next 

「TestVM」という名前の仮想マシンのIDEコントローラー1番ににDVDドライブを追加、「TestVM.iso」というISOイメージディスクを読み込ませています。
大まかな流れとしては、まずIDEコントローラーの1番を取得します。
取得したコントローラーの1番にDVDドライブを接続しています。
20103629060455

ほとんどハードディスクの追加と同じ手順ですね。
今回も細かい説明は省いていますが、詳細などなんなりとご質問ください。

コメント

コメントの投稿


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

トラックバック

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

FC2Ad