是否可以使用 Win32_NetworkAdapterConfiguration.EnableStatic() 设置多个 IP 地址?
我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通过使用 IP 数组和匹配的子网掩码数组使其工作。以下是 A 类私有子网的示例。
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.
尝试使用强制转换:
$ips 实际上不是字符串数组;它是一个对象数组。有时,powershell 的活页夹会与数组有点混淆,因为其中存在消歧的微妙之处,这些微妙之处比未经训练的人第一次看到的要复杂得多。
-奥辛
Try using a cast:
$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
为了成功调用,需要有一个与网络掩码匹配的字符串数组......
所以例如:
这就是第二篇文章中的示例起作用的原因......
for the call to be successful, there needs to be a matching string array for the netmask....
so ex:
that's why the example in the 2nd post works...