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
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!"
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With