【问题标题】:How to change gateway, subnet of a network adapter of a virtual Machine inside HyperV via WMI?如何通过 WMI 更改 HyperV 内部虚拟机网络适配器的网关、子网?
【发布时间】:2011-07-16 20:28:21
【问题描述】:

大家好,

我的问题是: 1. 如何在hyper-v中通过WMI和C#编程设置虚拟机内部网络适配器的子网掩码、DNS地址和网关地址?

注意:我已成功添加和检索 IP 地址,如 here 所述

请帮忙谢谢

史蒂夫

【问题讨论】:

  • 你有如何做到这一点的任何代码示例吗?我也有同样的问题,正在寻找解决方案,谢谢

标签: c# wmi gateway hyper-v subnet


【解决方案1】:

Hyper v 不支持该功能,其暴露的 SCVMM 服务没有任何方法可以执行此操作。您必须手动执行此操作。

【讨论】:

    【解决方案2】:

    我一直在寻找同样的东西并撞到了这个,

    http://blogs.msdn.com/b/taylorb/archive/2014/11/03/setting-guest-ip-addresses-from-the-host.aspx

    可以设置 IP 以及子网和网关,但最低要求是,

    • Windows 8 [仅限桌面应用]
    • Windows Server 2012 [仅限桌面应用]

    【讨论】:

      【解决方案3】:

      如果您使用的是 Server 2012 或更高版本,我会使用 @slayerbot 的链接,但如果您不是 2012 或更高版本,您可以查看 Win32_NetworkAdapterConfiguration 类的方法。

      https://msdn.microsoft.com/en-us/library/aa394217(v=vs.85).aspx#methods

      即Win32_NetworkAdapterConfiguration的SetDNSServerSearchOrder EnableStatic方法。

      如果您不更改 IP 地址,我将首先查询当前 IP 地址,然后在“EnableStatic”inParams 中重复使用该地址。

      另外请注意,您需要知道要更改的 NIC 的索引。

      Imports System.Globalization
      Imports System.Management
      
      Module SetDNSServerSearchOrder
      
          Dim UserName As String = "DOMAIN\UserName"
          Dim Password As String = "Passw0rd1"
      
          Public Sub Main()
              Dim VmName As String = "HYPERVGUESTOS1"                     ' Name of the VM to change NetworkAdapter on
              Dim NicIndex As Int16 = 0                                   ' Index of the network adapter to change
              Dim DnsList As String() = {"192.168.0.10", "192.168.0.11"}  ' List of DNS IP Addresses
              Dim Gateway As String = "192.168.0.1"                       ' Gateway IP address
              Dim IPAddress As String = "192.168.1.101"                   ' New (or old) IP Address
              Dim Subnet As String = "255.255.254.0"                      ' Subnet mask 
      
              ' Get the network adapter to configure
              Dim NetworkAdapter As ManagementObject = GetAdapter(VmName, NicIndex)
      
              ' Set DNS Server search order, then change the gateway, then IP/Subnet
              changeDNS(NetworkAdapter, DnsList)
              ChangeGateway(NetworkAdapter, GateWay)
              changeIP(NetworkAdapter, IPAddress, Subnet)
          End Sub
      
          Private Function GetAdapter(VmName As String, NicIndex As Int16) As ManagementObject
              Dim Options As New ConnectionOptions With {
                  .Username = UserName,
                  .Password = Password
              }
      
              Dim Scope As New ManagementScope("\\" & VmName & "\Root\CIMV2", Options)
              Dim Query As New SelectQuery("SELECT * FROM Win32_NetworkAdapterConfiguration.Index=" & NicIndex & "")
              Using Searcher As New ManagementObjectSearcher(Scope, Query)
                  Using Collection As ManagementObjectCollection = Searcher.Get()
                      If Collection.Count = 0 Then
                          Throw New ManagementException(String.Format(CultureInfo.CurrentCulture, "No network adapter could be found with index ""{0}"" on host ""{1}""" & NicIndex, VmName))
                      End If
      
                      For Each NetworkAdapter As ManagementObject In Collection
                          Return NetworkAdapter
                          Exit For
                      Next
                  End Using
              End Using
              Return Nothing
          End Function
      
          Private Sub changeDNS(NetworkAdapter As ManagementObject, DnsList As String())
              Using inParams As ManagementObject = NetworkAdapter.GetMethodParameters("SetDNSServerSearchOrder")
                  inParams("DNSServerSearchOrder") = DnsList
      
                  Using outParams As ManagementObject = NetworkAdapter.InvokeMethod("SetDNSServerSearchOrder", inParams, Nothing)
                      ' Validate the job output here
                  End Using
              End Using
          End Sub
      
          Private Sub changeGateway(NetworkAdapter As ManagementObject, Gateway As String)
              Using inParams As ManagementObject = NetworkAdapter.GetMethodParameters("SetGateways")
                  inParams("DefaultIPGateway") = Gateway
      
                  Using outParams As ManagementObject = NetworkAdapter.InvokeMethod("SetGateways", inParams, Nothing)
                      ' Validate the job output here
                  End Using
              End Using
          End Sub
      
          Private Sub changeIP(NetworkAdapter As ManagementObject, IPAddress As String, Subnet As String)
              Using inParams As ManagementObject = NetworkAdapter.GetMethodParameters("EnableStatic")
                  inParams("IPAddress") = IPAddress
                  inParams("SubnetMask") = Subnet
      
                  Using outParams As ManagementObject = NetworkAdapter.InvokeMethod("EnableStatic", inParams, Nothing)
                      ' Validate the job output here
                  End Using
              End Using
          End Sub
      End Module
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多