Freigeben über


PowerShell - Passing Parameters as Variables using Remote Management and Invoke-Command

Recently I had a need to write a PowerShell script that could accept a set of parameters at script launch, and pass those parameters to an inline scriptblock to allow the input parameters to act as variables in a set of commands against a remote server.  Looking at the documentation for the Invoke-Command cmdlet I saw that there was a parameter named ArgumentList, and looking at its description it seemed like the perfect fit for what I was looking to do.  The description for ArgumentList states that it, “Supplies the values of local variables in the command. The variables in the command are replaced by these values before the command is run on the remote computer. Enter the values in a comma-separated list. Values are associated with variables in the order that they are listed.”

Looking at the syntax and examples for the ArgumentList parameter, I was unable to find any details for using it with a set of input parameters, so once I was able to pull something together that worked I figured it would be worth to share a full example of this functionality in action.

Example script basicremoting.ps1

001002003004005006007008009010011012013014015016017 #.\BasicRemoting.ps1 -remoteserver LAB01 -eventloginput Application -numinput 10 Param (   [string]$remoteserver,   [string]$eventloginput,   [string]$numinput ) $ScriptBlockContent = { $eventlog = $args[0] $num = $args[1] get-eventlog -logname $eventlog -newest $num } Invoke-Command -Computer $remoteserver -ScriptBlock $ScriptBlockContent -ArgumentList $eventloginput, $numinput #End

Comments

  • Anonymous
    December 10, 2015
    very helpfull.
  • Anonymous
    August 12, 2016
    Thanks Ralph, very neat and helpful. Exactly what I needed.
  • Anonymous
    November 25, 2016
    Hi Ralph, to say ArgumentList is misleading would be an understatement :DIt makes working with these embedded sessions a real pain :-(However, thank you for your nice and simple, but very clear example!
  • Anonymous
    March 26, 2017
    The comment has been removed
  • Anonymous
    March 29, 2017
    Couldn't this work as well?Param( [string]$remoteserver, [string]$eventloginput, [string]$numinput)$ScriptBlockContent ={param ($eventloginput, $numinput)$eventlog = $eventloginput$num = $numinputget-eventlog -logname $eventlog -newest $num}Invoke-Command -Computer $remoteserver -ScriptBlock $ScriptBlockContent -ArgumentList $eventloginput, $numinput#End
  • Anonymous
    January 24, 2018
    Thanks was searching for DAYS before I found your post. Hadn't written any powershell since it came out. Was struggling to say the least. Implemented and worked great! Thanks!
  • Anonymous
    March 25, 2018
    Works great! Thanks for the example, which I found after having some initial problems passing the input arguments. I am using PowerShell to remote control a USB DAC, which is connected with RS232 to another computer. Using stored credentials, I have shortcuts on my desktop for changing volume, input etc.