Powershell - Pass multiple parameters to Invoke-Command
Asked Answered
K

2

8

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
Kiangsu answered 4/10, 2016 at 3:37 Comment(0)
S
13

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.

Saporous answered 4/10, 2016 at 9:14 Comment(0)
I
0

You may have an issue with the way you're expanding your variables and hence it may appear the args are not being passed correctly, when I'm debugging I simply use write to test the output. For example:

Invoke-Command -ComputerName localhost -ScriptBlock {write "C:\Program Files\Veritas\NetBackup\bin\admincmd\bpplinfo.exe CC0SITE_VMW_BRON -set -L -M CC0SITEb0100d0a.s0SITE.CC.DOMAIN.com"}
Invoke-Command -ComputerName localhost -ScriptBlock {write "C:\Program Files\Veritas\NetBackup\bin\admincmd\bpplinfo.exe $($args[0])0$($args[1])_VMW_BRON -set -L -M $($args[0])0$($args[1])b0100d0a.s0$($args[1]).$($args[0]).DOMAIN.com"} -Args "CC", "SITE" 
Infrared answered 4/10, 2016 at 9:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.