开发者

How to change gateway, subnet of a network adapter of a virtual Machine inside HyperV via WMI?

Helo all,

My Question is: 1. How to set Subnet Mask , DNS address and Gateway ad开发者_JAVA百科dress of network adapter inside a virtual machine in hyper-v programatically via WMI and C#?

Note: I am successfully able to add and retireve IP Address as described here

Kindly help Thanks

Steve


Hyper v don't have support for that , its exposed SCVMM service doesn't have any method to do that.you have to do that manually .


I was looking out for the same and bumped into this,

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

Setting IP along with subnet and gateway is possible, but minimum requirements are,

  • Windows 8 [desktop apps only]
  • Windows Server 2012 [desktop apps only]


If you are on Server 2012 or higher, I would go with @slayerbot's link, but if you are not on 2012 or higher, you could look into methods of the Win32_NetworkAdapterConfiguration class.

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

Namely the SetDNSServerSearchOrder EnableStatic methods of Win32_NetworkAdapterConfiguration.

If you are not changing the IP address, I would first query for the current IP address, then reuse that in the "EnableStatic" inParams.

Also note, you will need to know the Index of the NIC you want to change.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜