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

时间:2010-05-26 15:39:13

标签: .net powershell wmi

我在使用WMI但可以在PowerShell中确认它的Visual Basic程序中遇到此问题。显然,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中,它更复杂,必须传递数组,但该方法似乎只考虑每个数组的第一个元素。

我是否再次感到困惑或者这里有什么逻辑?

3 个答案:

答案 0 :(得分:2)

我通过使用具有匹配的子网掩码阵列的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)

答案 1 :(得分:1)

尝试使用演员:

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

$ ips实际上不是字符串数组;它是一个对象数组。有时,powershell的活页夹与数组有点混淆,因为消歧的微妙之处比第一次遇到未经训练的眼睛更复杂。

-Oisin

答案 2 :(得分:0)

要使调用成功,网络掩码需要匹配的字符串数组....

所以前:

$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)

这就是为什么第二篇文章中的例子有效......

相关问题