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