是否可以使用 Win32_NetworkAdapterConfiguration.EnableStatic() 设置多个 IP 地址?

发布于 2024-09-02 21:45:55 字数 820 浏览 5 评论 0原文

我在使用 WMI 的 Visual Basic 程序中遇到了这个问题,但可以在 PowerShell 中确认它。显然,EnableStatic() 方法只能用于设置一个 IP 地址,尽管其参数 IP 地址和子网掩码都是数组。

即,

$a=get-wmiobject win32_networkadapterconfiguration -computername myserver

这为我提供了“myserver”上所有网络适配器的数组。选择特定的一个(在本例中为 $a=$a[14] )后,我可以运行具有此签名的 $a.EnableStatic()

System.Management.ManagementBaseObject EnableStatic(System.String[] IPAddress, System.String[] SubnetMask)

我认为这意味着我可以像这样设置多个 IP 地址:

$ips="192.168.1.42","192.168.1.43"
$a.EnableStatic($ips,"255.255.255.0")

但此调用失败。然而,这个调用是有效的:

$a.EnableStatic($ips[0],"255.255.255.0")

在我看来,EnableStatic() 确实采用两个字符串而不是两个字符串数组作为参数。

在 Visual Basic 中,情况更为复杂,必须传递数组,但该方法似乎仅考虑每个数组的第一个元素。

我又困惑了还是这里有一些逻辑?

I ran into this problem in a Visual Basic program that uses WMI but could confirm it in PowerShell. Apparently the EnableStatic() method can only be used to set one IP address, despite taking two parameters IP address(es) and subnetmask(s) that are arrays.

I.e.

$a=get-wmiobject win32_networkadapterconfiguration -computername myserver

This gets me an array of all network adapters on "myserver". After selecting a specific one ($a=$a[14] in this case), I can run $a.EnableStatic() which has this signature

System.Management.ManagementBaseObject EnableStatic(System.String[] IPAddress, System.String[] SubnetMask)

I thought this implies that I could set several IP addresses like this:

$ips="192.168.1.42","192.168.1.43"
$a.EnableStatic($ips,"255.255.255.0")

But this call fails. However, this call works:

$a.EnableStatic($ips[0],"255.255.255.0")

It looks to me as if EnableStatic() really takes two strings rather than two arrays of strings as parameters.

In Visual Basic it's more complicated and arrays must be passed but the method appears to take into account only the first element of each array.

Am I confused again or is there some logic here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剑心龙吟 2024-09-09 21:45:55

我通过使用 IP 数组和匹配的子网掩码数组使其工作。以下是 A 类私有子网的示例。

$range = 2..254
$DNS = "8.8.8.8","8.8.4.4"
$gateway = "10.0.0.1"
$registerDns = $true
$IPs = @()
$mask = @()
foreach ($end in $range) {
    $IPs += "10.0.0.$end"
    $mask += "255.0.0.0"
}

$netcon = "Local Area Connection"
$index = (gwmi Win32_NetworkAdapter | where {$_.netconnectionid -eq $NetCon}).InterfaceIndex
$NetInterface = Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.InterfaceIndex -eq $index}

$NetInterface.EnableStatic($ips, $mask)
$NetInterface.SetGateways($gateway)
$NetInterface.SetDNSServerSearchOrder($dns)
$NetInterface.SetDynamicDNSRegistration($registerDns)

I got it working by using an array of IPs with a matched array of subnet masks. Here is an example for a Class A private subnet.

$range = 2..254
$DNS = "8.8.8.8","8.8.4.4"
$gateway = "10.0.0.1"
$registerDns = $true
$IPs = @()
$mask = @()
foreach ($end in $range) {
    $IPs += "10.0.0.$end"
    $mask += "255.0.0.0"
}

$netcon = "Local Area Connection"
$index = (gwmi Win32_NetworkAdapter | where {$_.netconnectionid -eq $NetCon}).InterfaceIndex
$NetInterface = Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.InterfaceIndex -eq $index}

$NetInterface.EnableStatic($ips, $mask)
$NetInterface.SetGateways($gateway)
$NetInterface.SetDNSServerSearchOrder($dns)
$NetInterface.SetDynamicDNSRegistration($registerDns)
变身佩奇 2024-09-09 21:45:55

尝试使用强制转换:

$a.EnableStatic([string[]]$ips,"255.255.255.0") 

$ips 实际上不是字符串数组;它是一个对象数组。有时,powershell 的活页夹会与数组有点混淆,因为其中存在消歧的微妙之处,这些微妙之处比未经训练的人第一次看到的要复杂得多。

-奥辛

Try using a cast:

$a.EnableStatic([string[]]$ips,"255.255.255.0") 

$ips is not actually a string array; it's an object array. Sometimes powershell's binder gets a bit confused with arrays as there are disambiguation subtleties that are more complex than first meets the untrained eye.

-Oisin

客…行舟 2024-09-09 21:45:55

为了成功调用,需要有一个与网络掩码匹配的字符串数组......

所以例如:

$ip = "10.10.10.10"
$ip += "10.10.10.11"
$ip += "10.10.10.12"
$mask = "255.255.255.0"
$mask += "255.255.255.0
$mask += "255.255.255.0

$nic.enablestatic($ip,$mask)

这就是第二篇文章中的示例起作用的原因......

for the call to be successful, there needs to be a matching string array for the netmask....

so ex:

$ip = "10.10.10.10"
$ip += "10.10.10.11"
$ip += "10.10.10.12"
$mask = "255.255.255.0"
$mask += "255.255.255.0
$mask += "255.255.255.0

$nic.enablestatic($ip,$mask)

that's why the example in the 2nd post works...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文