Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell - Pass multiple parameters to Invoke-Command

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
like image 590
itsmrmarlboroman2u Avatar asked Oct 04 '16 03:10

itsmrmarlboroman2u


People also ask

How do I pass multiple parameters to a PowerShell function?

Refer below PowerShell script for the above problem statement to pass multiple parameters to function in PowerShell. Write-Host $TempFile "file already exists!" Write-Host -f Green $TempFile "file created successfully!" Write-Host -f Green $FolderName "folder created successfully!"

How do you pass parameters to invoke in PowerShell?

To pass the argument in the Invoke-command, you need to use -ArgumentList parameter. For example, we need to get the notepad process information on the remote server.

How do you pass multiple parameters to a function?

Note that when you are working with multiple parameters, the function call must have the same number of arguments as there are parameters, and the arguments must be passed in the same order.

How do you pass parameters to invoke expressions?

The only parameter Invoke-Expression has is Command . There is no native way to pass parameters with Invoke-Expression . However, instead, you can include them in the string you pass to the Command parameter.


1 Answers

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.

like image 97
Dieter Gobeyn Avatar answered Oct 07 '22 01:10

Dieter Gobeyn