Powershell - Pass multiple parameters to Invoke-Command

itsmrmarlboroman2u picture itsmrmarlboroman2u · Oct 4, 2016 · Viewed 21k times · Source

I'm trying to write a one-liner to leverage some of the capabilities of netbackup remotely. I know how to pass parameters to Invoke Command using -args[0] and [1] at the end, with repeating parameters. An example of what I'm trying to accomplish:

CC = Country Code (Will repeat due to the naming conventions

SS = Site (Also repeats due to naming convention)

Invoke-Command -ComputerName RemoteServer -ScriptBlock {& "C:\Program Files\Veritas\NetBackup\bin\admincmd\bpplinfo.exe" CC0SITE_VMW_BRON -set -L -M CC0SITEb0100d0a.s0SITE.CC.DOMAIN.COM} 

After getting user-input and declaring the parameters, it doesn't seem to pass to the invoke-command

Invoke-Command -ComputerName RemoteServer -ScriptBlock {& "C:\Program Files\Veritas\NetBackup\bin\admincmd\bpplinfo.exe" $args[0]0$args[1]_VMW_BRON -L -M $args[0]0$args[1]b0100d0a.s0$args[1].$args[0].DOMAIN.com} -Args $CCode, $Site

Answer

Dieter Gobeyn picture Dieter Gobeyn · Oct 4, 2016

Use param($val1,...) inside the scriptblock to pass the arguments.

Invoke-Command -ComputerName 'SERVERNAME' -ScriptBlock {
param($argument1, $argument2) #<--- this is required!
 write-host $CCode
 write-host $Site
} -ArgumentList ($argument1, $argument2)

More information and syntax can be found at ArgumentList (alias Args) section for Invoke-Command cmdlet.