WIN1@Codename

Codename

Hyper-V をプログラムでどうにかしてみる VBScript その3 (ネットワークの設定)

続きまして、ネットワークの設定となります。
VBScriptもVB.NET同様、設定項目によりコードが徐々に複雑になってきますね。

Option Explicit

Dim strServerName, strVMName,  strNetworkName, objWMIService, objManagementService
strServerName = "."
strVMName = "TestVM"  
strNetworkName = "LAN - 仮想ネットワーク"
Set objWMIService = GetObject("winmgmts:\\" & strServerName & "\root\virtualization")
Set objManagementService = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemManagementService").ItemIndex(0)

Call SetNetworkAdapter(strVMName, strNetworkName)

Function SetNetworkAdapter(strVMName, strNetworkName)
    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 objVirtualSwitch
    Set objVirtualSwitch = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSwitch WHERE ElementName = '" & strNetworkName & "'").ItemIndex(0)
   
    Dim objInParam, objOutParams
    Set objInParam = objVirtualSwitchManagementService.Methods_("CreateSwitchPort").InParameters.SpawnInstance_()
    objInParam.Name = Replace(Replace(Replace(FormatDateTime(Now, 0), "/", ""), ":", ""), " ", "")
    objInParam.FriendlyName = strNetworkName
    objInParam.VirtualSwitch = objVirtualSwitch.Path_.Path
    Set objOutParams = objVirtualSwitchManagementService.ExecMethod_("CreateSwitchPort", objInParam)

    Dim objSwitchPort
    Set objSwitchPort = objWMIService.Get(objOutParams.CreatedSwitchPort)

    Dim objEmulatedEthernetPortSettingData
    Set objEmulatedEthernetPortSettingData = objWMIService.ExecQuery("SELECT * FROM Msvm_SyntheticEthernetPortSettingData WHERE InstanceID like '%Default%'").ItemIndex(0)
   
    Dim objResourcePool
    Set objResourcePool = objWMIService.ExecQuery("SELECT * FROM Msvm_ResourcePool WHERE ResourceType = 10 AND ResourceSubType = 'Microsoft Synthetic Ethernet Port' 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 objDefaultNic
    If objSettingsDefineCapabilities.ValueRole = 0 Then
        Set objDefaultNic = objWMIService.Get(objSettingsDefineCapabilities.PartComponent)
    End If

    Dim objVirtualSystemManagementService
    Set objVirtualSystemManagementService = objWMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemManagementService").ItemIndex(0)
    
    Dim strConnection
    strConnection = Array(1)
    strConnection(0) = objSwitchPort.Path_.Path
    objDefaultNic.Connection = strConnection
    objDefaultNic.StaticMacAddress = False
    objDefaultNic.ElementName = "ネットワーク アダプター"

    Dim objTypeLib
    Set objTypeLib = CreateObject("Scriptlet.TypeLib")
    Dim strVirtualSystemIdentifiers
    strVirtualSystemIdentifiers = Array(1)

    strVirtualSystemIdentifiers(0) =  objTypeLib.Guid
    objDefaultNic.VirtualSystemIdentifiers = strVirtualSystemIdentifiers
      
    Dim strResourceSettingData
    strResourceSettingData = Array(1)
    strResourceSettingData(0) = objDefaultNic.GetText_(1)  

    Dim objInParam2, objOutParams2
    Set objInParam2 = objManagementService.Methods_("AddVirtualSystemResources").InParameters.SpawnInstance_()
    objInParam2.ResourceSettingData = strResourceSettingData
    objInParam2.TargetSystem = objComputerSystem.Path_.Path
    Set objOutParams2 = objManagementService.ExecMethod_("AddVirtualSystemResources", objInParam2)

    Call JobCompleted(objOutParams2, 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

ふぅ~、とこんな感じですね。
「TestVM」という仮想マシンに「LAN - 仮想ネットワーク」と名づけた仮想ネットワーク設定を接続しています。
「LAN」仮想ネットワーク設定は事前にHyper-Vマネージャーの仮想ネットワークマネージャーより作成してあることとします。

コメント

コメントの投稿


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

トラックバック

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

FC2Ad