The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

MikeShepard (14) [Avatar] Offline
#1
In section 39.2.4 you said:

"This is not satisfactory because you have to adopt different techniques depending on the
number of DNS servers you need to put into the property. This is NOT a PowerShell issue – it
is a WMI issue because the IP address that we saw last time also takes an array and it was
very happy with a single value."

What you're seeing is how Powershell array unrolling interacts with -argumentlist (which takes a single array parameter) and the SetDNSServerSearchOrder method (whose first parameter is an array).

You can see the same result using invoke-command to see that this behavior isn't related to WMI.

function test-args{
param([string[]]$stuff,$remainder)
$stuff
}

#calling the function
test-args a, b, c, d

#using invoke-command
Invoke-Command {param($a) test-args $a } -ArgumentList a,b,c,d

#using invoke-command
Invoke-Command {param($a) test-args $a } -ArgumentList (,@('a','b','c','d'))
MikeShepard (14) [Avatar] Offline
#2
Re: ArgumentList and Array arguments (e.g. Invoke-WMIMethod) in section 39.2.4
I forgot to describe the output.

Calling the function directly displays a,b,c,d (on separate lines)

Calling the function through invoke-command (and with a scriptblock) only outputs "a" because the array was unrolled to match the parameter list. So "a" was passed as the first parameter, "b" was passed as the second, etc.

Calling the function through invoke-command and with the unary array operator forces the entire array to be passed as the first argument.
mjolinor (5) [Avatar] Offline
#3
Re: ArgumentList and Array arguments (e.g. Invoke-WMIMethod) in section 39.2.4
Sometimes it's cleaner to just create a new script block using local variable values embedded than to use variables in the script block and pass the values through $args.

IMHO
MikeShepard (14) [Avatar] Offline
#4
Re: ArgumentList and Array arguments (e.g. Invoke-WMIMethod) in section 39.2.4
Normally I would probably do that. Here, though, the point was to pass a parameter through -argumentlist (to point out that PowerShell was doing the unrolling).
MikeShepard (14) [Avatar] Offline
#5
Re: ArgumentList and Array arguments (e.g. Invoke-WMIMethod) in section 39.2.4
I just realized that the invoke-command examples could be simpler (without the function):

#using invoke-command (and powershell unrolling the array)
Invoke-Command {param($a) $a } -ArgumentList a,b,c,d

#using invoke-command (without unrolling)
Invoke-Command {param($a) $a } -ArgumentList (,@('a','b','c','d'))