Если вы используете Server 2012 или новее, я бы пошел со ссылкой @ slayerbot, но если вы не в 2012 или новее, вы можете посмотреть методы класса Win32_NetworkAdapterConfiguration.
https://msdn.microsoft.com/en-us/library/aa394217(v=vs.85).aspx#methods
А именно, SetDNSServerSearchOrder EnableStatic методы Win32_NetworkAdapterConfiguration.
Если вы не меняете IP-адрес, я сначала запросил бы текущий IP-адрес, а затем повторно использовал его в «EnableStatic» inParams.
Также обратите внимание, что вам нужно знать индекс сетевой карты, которую вы хотите изменить.
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