views:

175

answers:

1

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?

+1  A: 

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

x0n
I checked the types, you are correct. However, now I am always getting a return value of 2147786788 when I do $nic.EnableStatic("192.168.1.43","255.255.255.0"). I think this is an unrelated issue but makes testing difficult.
Andrew J. Brehm
Actually, calling EnableStatic() with a string[] $ips gives me result 90 ("Parameter out of bounds."). Any ideas?
Andrew J. Brehm
try: $a.EnableStatic([string[]]$ips, [string[]]@("255.255.255.0"))
x0n
I did a $ips=[string[]]$ips first.
Andrew J. Brehm
Returns 90 again...
Andrew J. Brehm
Doesn't work. Any other ideas? :-(
Andrew J. Brehm
This still doesn't work.
Andrew J. Brehm