如何通过WMI在HyperV中更改虚拟机网络适配器的网关,子网?

时间:2011-07-16 20:28:21

标签: c# wmi gateway hyper-v subnet

Helo all,

我的问题是: 1.如何通过WMI和C#以编程方式在虚拟机内设置网络适配器的子网掩码,DNS地址和网关地址?

注意:我已成功添加和退出IP地址,如here

所述

请帮助谢谢

史蒂夫

3 个答案:

答案 0 :(得分:2)

Hyper v没有支持,它暴露的SCVMM服务没有任何方法可以做到这一点。你必须手动完成。

答案 1 :(得分:0)

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

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

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

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

答案 2 :(得分:0)

如果您使用的是Server 2012或更高版本,我会使用@sieyerbot的链接,但如果您不在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