大家好,
我的问题是: 1. 如何通过 WMI 和 C# 以编程方式在 hyper-v 中设置虚拟机内部网络适配器的子网掩码、DNS 地址和网关地址?
注意:我可以成功地添加和检索 IP 地址,如此处所述
请帮忙谢谢
史蒂夫
Hyper v 不支持该功能,其公开的 SCVMM 服务没有任何方法可以做到这一点。您必须手动完成。
我一直在寻找同样的东西并碰到了这个,
http://blogs.msdn.com/b/taylorb/archive/2014/11/03/setting-guest-ip-addresses-from-the-host.aspx
可以设置 IP 以及子网和网关,但最低要求是,
如果您使用的是 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