WIN1@Codename

Codename

Hyper-Vの新機能WMI provider (V2) vol.2

仮想スイッチの作成方法

まずは仮想スイッチの作成方法です。Hyper-Vでは作成できる仮想スイッチが3種類あります。
プライベート:
 物理コンピューター上で実行される仮想マシンのみが使用する仮想スイッチ。VM間の通信のみで使用します。

内部:
 物理コンピューター上で実行される仮想マシンのみで使用できる、仮想マシンと物理コンピューター間専用の仮想スイッチ。内部の仮想スイッチでは、物理ネットワーク接続に接続できません。VMとホスト間の通信が必要な場合に使用します。

外部:
 仮想マシンが物理ネットワークにアクセスするために、物理ネットワークアダプタにバインドする仮想スイッチ。直接インターネットに接続する場合などに使用します。

 WMI経由で仮想スイッチを設定する場合、これら3種類の仮想スイッチごとに、細かいところで設定項目が異なります。ポイントをふまえて3つ一気にサンプルをご紹介します。

   1:  Imports System.Management
   2:  
   3:  Module GMOReport
   4:  
   5:      Sub Main()
   6:          Dim strUser As String = ""
   7:          Dim strPass As String = ""
   8:          Dim objManagementScope As ManagementScope = ConnectManagementScope("win2012.local", strUser, strPass)
   9:          'Call CreatePrivateSwitch(objManagementScope, "Private Switch", "プライベート専用")
  10:          'Call CreateInternalSwitch(objManagementScope, "win2012", "Internal Switch", "内部専用")
  11:          'Call CreateExternalSwitch(objManagementScope, "External Switch", "外部専用", "Broadcom BCM5709C NetXtreme II")
  12:      End Sub
  13:  
  14:      Function ConnectManagementScope(ByVal strServer As String, ByVal strAccount As String, ByVal strPassword As String) As ManagementScope
  15:          Dim objConnectionOptions As New ConnectionOptions()
  16:          objConnectionOptions.Impersonation = ImpersonationLevel.Impersonate
  17:          objConnectionOptions.EnablePrivileges = True
  18:          objConnectionOptions.Username = strAccount
  19:          objConnectionOptions.Password = strPassword
  20:          Dim objManagementScope As New ManagementScope("\\" + strServer + "\root\virtualization\v2", objConnectionOptions)
  21:          objManagementScope.Connect()
  22:          Return (objManagementScope)
  23:      End Function
  24:  
  25:      Function CreatePrivateSwitch(ByVal objManagementScope As ManagementScope, ByVal strSwitchName As String, strNote As String) As Boolean
  26:          Dim strVirtualEthernetSwitchSettingData As String = ""
  27:          Dim objVirtualEthernetSwitchSettingData As New ManagementClass(objManagementScope, New ManagementPath("Msvm_VirtualEthernetSwitchSettingData"), Nothing)
  28:          Dim objVirtualEthernetSwitchSettingDataInstance As ManagementObject = objVirtualEthernetSwitchSettingData.CreateInstance
  29:          objVirtualEthernetSwitchSettingDataInstance("ElementName") = strSwitchName
  30:          objVirtualEthernetSwitchSettingDataInstance("Notes") = New String() {strNote}
  31:          strVirtualEthernetSwitchSettingData = objVirtualEthernetSwitchSettingDataInstance.GetText(TextFormat.CimDtd20)
  32:  
  33:          For Each objVirtualEthernetSwitchManagementService As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_VirtualEthernetSwitchManagementService")).Get
  34:              Dim objParams As ManagementBaseObject = objVirtualEthernetSwitchManagementService.GetMethodParameters("DefineSystem")
  35:              objParams("SystemSettings") = strVirtualEthernetSwitchSettingData
  36:              objParams("ResourceSettings") = Nothing
  37:              Dim objManagementBaseObject As ManagementBaseObject = objVirtualEthernetSwitchManagementService.InvokeMethod("DefineSystem", objParams, Nothing)
  38:              Return JobComplete(objManagementBaseObject, objManagementScope)
  39:          Next
  40:      End Function
  41:  
  42:      Function CreateInternalSwitch(ByVal objManagementScope As ManagementScope, ByVal strServerName As String, ByVal strSwitchName As String, strNote As String) As Boolean
  43:          Dim objComputerSystem As ManagementObject = Nothing
  44:          For Each objManagementObject As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName = '" & strServerName & "'")).Get
  45:              objComputerSystem = objManagementObject
  46:          Next
  47:  
  48:          Dim strVirtualEthernetSwitchSettingData As String = ""
  49:          Dim objVirtualEthernetSwitchSettingData As New ManagementClass(objManagementScope, New ManagementPath("Msvm_VirtualEthernetSwitchSettingData"), Nothing)
  50:          Dim objVirtualEthernetSwitchSettingDataInstance As ManagementObject = objVirtualEthernetSwitchSettingData.CreateInstance
  51:          objVirtualEthernetSwitchSettingDataInstance("ElementName") = strSwitchName
  52:          objVirtualEthernetSwitchSettingDataInstance("Notes") = New String() {strNote}
  53:          strVirtualEthernetSwitchSettingData = objVirtualEthernetSwitchSettingDataInstance.GetText(TextFormat.CimDtd20)
  54:  
  55:          Dim strEthernetPortAllocationSettingData As String = ""
  56:          Dim objEthernetPortAllocationSettingData As New ManagementClass(objManagementScope, New ManagementPath("Msvm_EthernetPortAllocationSettingData"), Nothing)
  57:          Dim objEthernetPortAllocationSettingDataInstance As ManagementObject = objEthernetPortAllocationSettingData.CreateInstance
  58:          objEthernetPortAllocationSettingDataInstance("ElementName") = strSwitchName
  59:          objEthernetPortAllocationSettingDataInstance("HostResource") = New String() {objComputerSystem.Path.Path}
  60:          strEthernetPortAllocationSettingData = objEthernetPortAllocationSettingDataInstance.GetText(TextFormat.CimDtd20)
  61:  
  62:          For Each objVirtualEthernetSwitchManagementService As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_VirtualEthernetSwitchManagementService")).Get
  63:              Dim objParams As ManagementBaseObject = objVirtualEthernetSwitchManagementService.GetMethodParameters("DefineSystem")
  64:              objParams("SystemSettings") = strVirtualEthernetSwitchSettingData
  65:              objParams("ResourceSettings") = New String() {strEthernetPortAllocationSettingData}
  66:              Dim objManagementBaseObject As ManagementBaseObject = objVirtualEthernetSwitchManagementService.InvokeMethod("DefineSystem", objParams, Nothing)
  67:              Return JobComplete(objManagementBaseObject, objManagementScope)
  68:          Next
  69:      End Function
  70:  
  71:      Function CreateExternalSwitch(ByVal objManagementScope As ManagementScope, ByVal strSwitchName As String, strNote As String, strExternalAdapterName As String) As Boolean
  72:          Dim strVirtualEthernetSwitchSettingData As String = ""
  73:          Dim objVirtualEthernetSwitchSettingData As New ManagementClass(objManagementScope, New ManagementPath("Msvm_VirtualEthernetSwitchSettingData"), Nothing)
  74:          Dim objVirtualEthernetSwitchSettingDataInstance As ManagementObject = objVirtualEthernetSwitchSettingData.CreateInstance
  75:          objVirtualEthernetSwitchSettingDataInstance("ElementName") = strSwitchName
  76:          objVirtualEthernetSwitchSettingDataInstance("Notes") = New String() {strNote}
  77:          strVirtualEthernetSwitchSettingData = objVirtualEthernetSwitchSettingDataInstance.GetText(TextFormat.CimDtd20)
  78:  
  79:          Dim objExternalEthernetPort As ManagementObject = Nothing
  80:          For Each objManagementObject As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_ExternalEthernetPort WHERE Name = '" & strExternalAdapterName & "'")).Get
  81:              objExternalEthernetPort = objManagementObject
  82:          Next
  83:  
  84:          Dim strEthernetPortAllocationSettingData As String = ""
  85:          Dim objEthernetPortAllocationSettingData As New ManagementClass(objManagementScope, New ManagementPath("Msvm_EthernetPortAllocationSettingData"), Nothing)
  86:          Dim objEthernetPortAllocationSettingDataInstance As ManagementObject = objEthernetPortAllocationSettingData.CreateInstance
  87:          objEthernetPortAllocationSettingDataInstance("ElementName") = strSwitchName
  88:          objEthernetPortAllocationSettingDataInstance("HostResource") = New String() {objExternalEthernetPort.Path.Path}
  89:          strEthernetPortAllocationSettingData = objEthernetPortAllocationSettingDataInstance.GetText(TextFormat.CimDtd20)
  90:  
  91:          For Each objVirtualEthernetSwitchManagementService As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_VirtualEthernetSwitchManagementService")).Get
  92:              Dim objParams As ManagementBaseObject = objVirtualEthernetSwitchManagementService.GetMethodParameters("DefineSystem")
  93:              objParams("SystemSettings") = strVirtualEthernetSwitchSettingData
  94:              objParams("ResourceSettings") = New String() {strEthernetPortAllocationSettingData}
  95:              Dim objManagementBaseObject As ManagementBaseObject = objVirtualEthernetSwitchManagementService.InvokeMethod("DefineSystem", objParams, Nothing)
  96:              Return JobComplete(objManagementBaseObject, objManagementScope)
  97:          Next
  98:      End Function
  99:  
 100:      Function JobComplete(ByVal objManagementBaseObject As ManagementBaseObject, ByVal objManagementScope As ManagementScope) As Boolean  
 101:          If objManagementBaseObject("ReturnValue") <> 0 Then
 102:              Dim strJobPath As String = objManagementBaseObject("Job")
 103:              Dim objJob As New ManagementObject(objManagementScope, New ManagementPath(strJobPath), Nothing)
 104:              objJob.Get()
 105:              Do While objJob("JobState") = 3 Or objJob("JobState") = 4
 106:                  System.Threading.Thread.Sleep(1000)
 107:                  objJob.Get()
 108:              Loop
 109:              If objJob("JobState") <> 7 Then
 110:                  Console.WriteLine("ErrorCode=" & objJob("ErrorCode") & " JobState=" & objJob("JobState"))
 111:                  Return False
 112:              Else
 113:                  Return True
 114:              End If
 115:          Else
 116:              Return True
 117:          End If
 118:      End Function
 119:  
 120:  End Module

CreatePrivateSwitch:
 プライベート仮想スイッチは一番シンプルな構文です。
26-31行目:
 仮想スイッチの設定項目を Msvm_VirtualEthernetSwitchSettingData クラスより取得して、「ElementName」で仮想スイッチ名、「Notes」で備考内容を定義します。
33-39行目:
 「Msvm_VirtualEthernetSwitchManagementService」クラスの「DefineSystem」メソッドで、「SystemSettings」には先ほど定義した「objVirtualEthernetSwitchSettingData」を、「ResourceSettings」にはNothingのパラメーターを設定し、実行します。
CreateInternalSwitch:
 内部仮想スイッチは、プライベート仮想スイッチに、「ResourceSettings」パラメーターを追加します。
55-60行目:
 「Msvm_EthernetPortAllocationSettingData」クラスの「ElementName」で仮想スイッチ名、「HostResource」で設定するホストを定義します。
62-68行目:
 プライベート仮想スイッチの作成時と同様に「Msvm_VirtualEthernetSwitchManagementService」クラスの「DefineSystem」メソッドを使用します。「ResourceSettings」には先ほど定義した「objEthernetPortAllocationSettingData」を設定します。

仮想マシンへのネットワークアダプタの追加

ExternalSwitch:
 外部仮想スイッチの場合は接続先を外部ネットワークに設定します。
79-82行目:
 外部ネットワークに接続された「Msvm_ExternalEthernetPort」クラスオブジェクトを取得します。
88行目:
 「Msvm_EthernetPortAllocationSettingData」の「ResourceSettings」パラメーターに、先ほど取得した「objExternalEthernetPort」を設定します。
Hyper-VマネージャのGUIから設定した場合と同じ設定内容で仮想スイッチが作成されます。

ネットワークの接続

次に、作成した仮想スイッチと仮想マシンを接続しましょう。
Hyper-Vマネージャでは、「ハードウェアの追加」から「ネットワークアダプタ」を選択して、「仮想スイッチ」を選択する手順にします。今回は先ほど作成した「Private Switch」と名前をつけたプライベートスイッチに接続します。

Windows Server 2012 R2のHyper-Vでは仮想マシンの世代を選択することができるようになりました。



コード自体は長くなりますが、ほとんどパターンの決まった構文となりますので、ポイントのみ、かい摘んでご説明します。

   1:  Imports System.Management
   2:  
   3:  Module GMOReport
   4:  
   5:      Sub Main()
   6:          Dim strUser As String = ""
   7:          Dim strPass As String = ""
   8:          Dim objManagementScope As ManagementScope = ConnectManagementScope("win2012.local", strUser, strPass)
   9:          'Call SetConnectNetworkAdapter(objManagementScope, "VM01", "Private Switch", "ネットワーク アダプター")
  10:          'Call RemoveNetworkAdapter(objManagementScope, "VM01", "ネットワーク アダプター")
  11:      End Sub
  12:  
  13:      Function ConnectManagementScope(ByVal strServer As String, ByVal strAccount As String, ByVal strPassword As String) As ManagementScope
  14:          Dim objConnectionOptions As New ConnectionOptions()
  15:          objConnectionOptions.Impersonation = ImpersonationLevel.Impersonate
  16:          objConnectionOptions.EnablePrivileges = True
  17:          objConnectionOptions.Username = strAccount
  18:          objConnectionOptions.Password = strPassword
  19:          Dim objManagementScope As New ManagementScope("\\" + strServer + "\root\virtualization\v2", objConnectionOptions)
  20:          objManagementScope.Connect()
  21:          Return (objManagementScope)
  22:      End Function
  23:  
  24:      Function SetConnectNetworkAdapter(ByVal objManagementScope As ManagementScope, ByVal strVMName As String, ByVal strSwitchName As String, strNetworkAdapterName As String) As Boolean
  25:          Dim objComputerSystem As ManagementObject = Nothing
  26:          For Each objManagementObject As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName = '" & strVMName & "'")).Get
  27:              objComputerSystem = objManagementObject
  28:          Next
  29:  
  30:          Dim objVirtualSystemsettingData As ManagementObject = Nothing
  31:          For Each objManagementObject As ManagementObject In objComputerSystem.GetRelated("Msvm_VirtualSystemSettingData")
  32:              If String.Compare(objManagementObject("ElementName").ToString, strVMName, True) = 0 Then objVirtualSystemsettingData = objManagementObject
  33:          Next
  34:  
  35:          Dim objResourcePool As ManagementObject = Nothing
  36:          Dim objNetworkAdapter As ManagementObject = Nothing
  37:          For Each objManagementObject As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_ResourcePool WHERE ResourceSubType = 'Microsoft:Hyper-V:Synthetic Ethernet Port' AND Primordial = True")).Get
  38:              objResourcePool = objManagementObject
  39:              Dim objAllocationCapabilitiesCollection As ManagementObjectCollection = objResourcePool.GetRelated("Msvm_AllocationCapabilities")
  40:              For Each objAllocationCapabilities As ManagementObject In objAllocationCapabilitiesCollection
  41:                  Dim objSettingsDefineCapabilitiesCollection As ManagementObjectCollection = objAllocationCapabilities.GetRelationships("Msvm_SettingsDefineCapabilities")
  42:                  For Each objSettingsDefineCapabilities As ManagementObject In objSettingsDefineCapabilitiesCollection
  43:                      If objSettingsDefineCapabilities("ValueRole") = 0 Then
  44:                          objNetworkAdapter = New ManagementObject(objSettingsDefineCapabilities("PartComponent").ToString())
  45:                          objNetworkAdapter.Scope = objManagementScope
  46:                          objNetworkAdapter.Get()
  47:                          objNetworkAdapter("VirtualSystemIdentifiers") = New String() {String.Format("{{{0}}}", Guid.NewGuid())}
  48:                          objNetworkAdapter("ElementName") = strNetworkAdapterName
  49:                          objNetworkAdapter("StaticMacAddress") = False
  50:                          Exit For
  51:                      End If
  52:                  Next
  53:              Next
  54:          Next
  55:  
  56:          Dim objAddedNetworkAdapter As ManagementObject = Nothing
  57:          For Each objVirtualSystemManagementService As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_VirtualSystemManagementService")).Get
  58:              Dim objParams As ManagementBaseObject = objVirtualSystemManagementService.GetMethodParameters("AddResourceSettings")
  59:              objParams("AffectedConfiguration") = objVirtualSystemsettingData.Path.Path
  60:              objParams("ResourceSettings") = New String() {objNetworkAdapter.GetText(TextFormat.WmiDtd20)}
  61:              Dim objManagementBaseObject As ManagementBaseObject = objVirtualSystemManagementService.InvokeMethod("AddResourceSettings", objParams, Nothing)
  62:              objAddedNetworkAdapter = New ManagementObject(objManagementBaseObject("ResultingResourceSettings")(0).ToString())
  63:          Next
  64:  
  65:          Dim objVirtualEthernetSwitch As ManagementObject = Nothing
  66:          For Each objManagementObject As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_VirtualEthernetSwitch WHERE ElementName = '" & strSwitchName & "'")).Get
  67:              objVirtualEthernetSwitch = objManagementObject
  68:          Next
  69:  
  70:          Dim objEthernetConnection As ManagementObject = Nothing
  71:          For Each objManagementObject As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_ResourcePool WHERE ResourceSubType = 'Microsoft:Hyper-V:Ethernet Connection' AND Primordial = True")).Get
  72:              objResourcePool = objManagementObject
  73:              Dim objAllocationCapabilitiesCollection As ManagementObjectCollection = objResourcePool.GetRelated("Msvm_AllocationCapabilities")
  74:              For Each objAllocationCapabilities As ManagementObject In objAllocationCapabilitiesCollection
  75:                  Dim objSettingsDefineCapabilitiesCollection As ManagementObjectCollection = objAllocationCapabilities.GetRelationships("Msvm_SettingsDefineCapabilities")
  76:                  For Each objSettingsDefineCapabilities As ManagementObject In objSettingsDefineCapabilitiesCollection
  77:                      If objSettingsDefineCapabilities("ValueRole") = 0 Then
  78:                          objEthernetConnection = New ManagementObject(objSettingsDefineCapabilities("PartComponent").ToString())
  79:                          objEthernetConnection.Scope = objManagementScope
  80:                          objEthernetConnection.Get()
  81:                          objEthernetConnection("Parent") = objAddedNetworkAdapter.Path.Path
  82:                          objEthernetConnection("HostResource") = New String() {objVirtualEthernetSwitch.Path.Path}
  83:                          Exit For
  84:                      End If
  85:                  Next
  86:              Next
  87:          Next
  88:  
  89:          For Each objVirtualSystemManagementService As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_VirtualSystemManagementService")).Get
  90:              Dim objParams As ManagementBaseObject = objVirtualSystemManagementService.GetMethodParameters("AddResourceSettings")
  91:              objParams("AffectedConfiguration") = objVirtualSystemsettingData.Path.Path
  92:              objParams("ResourceSettings") = New String() {objEthernetConnection.GetText(TextFormat.WmiDtd20)}
  93:              Dim objManagementBaseObject As ManagementBaseObject = objVirtualSystemManagementService.InvokeMethod("AddResourceSettings", objParams, Nothing)
  94:              Return JobComplete(objManagementBaseObject, objManagementScope)
  95:          Next
  96:      End Function
  97:  
  98:      Function RemoveNetworkAdapter(ByVal objManagementScope As ManagementScope, ByVal strVMName As String, strNetworkAdapterName As String) As Boolean
  99:          Dim objComputerSystem As ManagementObject = Nothing
 100:          For Each objManagementObject As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName = '" & strVMName & "'")).Get
 101:              objComputerSystem = objManagementObject
 102:          Next
 103:  
 104:          Dim objVirtualSystemsettingData As ManagementObject = Nothing
 105:          For Each objManagementObject As ManagementObject In objComputerSystem.GetRelated("Msvm_VirtualSystemSettingData")
 106:              If String.Compare(objManagementObject("ElementName").ToString, strVMName, True) = 0 Then objVirtualSystemsettingData = objManagementObject
 107:          Next
 108:  
 109:          Dim objEthernetPortAllocationSettingData As ManagementObject = Nothing
 110:          Dim objNetworkAdapter As ManagementObject = Nothing
 111:          'ネットワークアダプタが複数ある場合一覧から名前の一致するものを選択
 112:          For Each objManagementObject As ManagementObject In objVirtualSystemsettingData.GetRelated("Msvm_SyntheticEthernetPortSettingData")
 113:              If objManagementObject("ElementName") = strNetworkAdapterName Then
 114:                  objNetworkAdapter = objManagementObject
 115:                  For Each objManagementObject2 As ManagementObject In objVirtualSystemsettingData.GetRelated("Msvm_EthernetPortAllocationSettingData")
 116:                      objEthernetPortAllocationSettingData = objManagementObject2
 117:                      If objNetworkAdapter.Path.Path = objEthernetPortAllocationSettingData("Parent") Then
 118:                          Exit For
 119:                      End If
 120:                  Next
 121:              End If
 122:          Next
 123:  
 124:          For Each objVirtualSystemManagementService As ManagementObject In New ManagementObjectSearcher(objManagementScope, New ObjectQuery("SELECT * FROM Msvm_VirtualSystemManagementService")).Get
 125:              Dim objParams As ManagementBaseObject = objVirtualSystemManagementService.GetMethodParameters("RemoveResourceSettings")
 126:              objParams("ResourceSettings") = New String() {objNetworkAdapter.Path.Path}
 127:              Dim objManagementBaseObject As ManagementBaseObject = objVirtualSystemManagementService.InvokeMethod("RemoveResourceSettings", objParams, Nothing)
 128:              Return JobComplete(objManagementBaseObject, objManagementScope)
 129:          Next
 130:      End Function
 131:  
 132:      Function JobComplete(ByVal objManagementBaseObject As ManagementBaseObject, ByVal objManagementScope As ManagementScope) As Boolean  
 133:          If objManagementBaseObject("ReturnValue") <> 0 Then
 134:              Dim strJobPath As String = objManagementBaseObject("Job")
 135:              Dim objJob As New ManagementObject(objManagementScope, New ManagementPath(strJobPath), Nothing)
 136:              objJob.Get()
 137:              Do While objJob("JobState") = 3 Or objJob("JobState") = 4
 138:                  System.Threading.Thread.Sleep(1000)
 139:                  objJob.Get()
 140:              Loop
 141:              If objJob("JobState") <> 7 Then
 142:                  Console.WriteLine("ErrorCode=" & objJob("ErrorCode") & " JobState=" & objJob("JobState"))
 143:                  Return False
 144:              Else
 145:                  Return True
 146:              End If
 147:          Else
 148:              Return True
 149:          End If
 150:      End Function
 151:  
 152:  End Module

37行目:
 新規でネットワークアダプタを追加するために、「Msvm_ResourcePool」クラスからネットワークポートオブジェクトとなる「Microsoft:Hyper-V:Synthetic Ethernet Port」を選択します。
38-46行目:
 ネットワークアダプタを追加するためのプロパティーを設定します。
47行目:
 「VirtualSystemIdentifiers」は配列タイプのユニークな一意のID番号となりますので、New String() {String.Format("{{{0}}}", Guid.NewGuid())} で生成した値を割り当てます。
48行目:
 「ElementName」はHyper-Vマネージャでは常に" ネットワーク アダプター"という名前となりますが、WMIから作成する場合は、任意の名前を設定することが可能です。
49行目:
 「StaticMacAddress」はMacアドレスを固定で割り当てるか、動的に自動で割り当てるか、どちらかの設定となります。今回は「False」とします。
57-61行目:
 「Msvm_VirtualSystemManagementService」クラスの「AddResourceSettings」メソッドで追加します。仮想マシンにリソースを追加する場合は、ほぼこのパターンになります。「AffectedConfiguration」には先に取得した仮想マシンの「objVirtualSystemsettingData」を指定します。これは、どの仮想マシンにネットワークアダプタを追加するのかの指示です。「ResourceSettings」では先ほど作成した「objNetworkAdapter」を指定します。
62行目:
 仮想マシンにネットワークアダプタが追加されたら、「objAddedNetworkAdapter」に格納しておきます。
65-68行目:
 仮想スイッチの名前をキーとして仮想スイッチオブジェクトを取得します。
71行目:
 先ほどのネットワークアダプタ同様に、「Msvm_ResourcePool」クラスから今度はネットワーク接続のオブジェクトとなる「Microsoft:Hyper-V:Ethernet Connection」を選択します。
81行目:
 このネットワーク接続は、先ほどの「objAddedNetworkAdapter」に関連づけするので、「Parent」プロパティーに指定します。
82行目:
 接続先は仮想スイッチの名前をキーとして取得した「objVirtualEthernetSwitch」仮想スイッチです。
89行目:
 最後に「AddResourceSettings」メソッドで追加します。
92行目:
今度は「ResourceSettings」にはネットワーク接続のオブジェクト「objEthernetConnection」を指定します。 これでようやく、仮想マシンにネットワークアダプタが追加され、仮想スイッチに接続した状態になります。

ネットワークアダプタの削除方法

サンプルには「RemoveNetworkAdapter」として、追加したネットワークアダプタを削除するコードも紹介しています。ポイントとしては、

113行目:
 ネットワークアダプタの名前をキーとして削除するネットワークアダプタを選択します。
124-125行目:
 「Msvm_VirtualSystemManagementService」の「RemoveResourceSettings」メソッドで削除します。

今回は仮想スイッチの作成、仮想マシンへのネットワークアダプタの追加、ネットワークの接続、ネットワークアダプタの削除方法をご紹介しました。

コメント

コメントの投稿


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

トラックバック

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

FC2Ad