开发者

Can Win32_NetworkAdapterConfiguration.EnableStatic() be used to set more than one IP address?

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开发者_Go百科 take into account only the first element of each array.

Am I confused again or is there some logic here?


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)


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


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜