Search code examples
powershellparametersargumentsinvoke-command

Powershell: How to pass parameter with invoke-command and -filepath remotely?


so there's a lot of similar topics to this I've found, but I can't find anything that correlates with my issue.

invoke-command -computername $serverLocation -Credential $cred -filepath "C:\path\script2.ps1" -ArgumentList $configPath

I am calling a for a script that is stored on my local machine to run on another server. Everything works as intended except for passing the "configPath" variable. This is how I initiate the script -

. .\script1.ps1 -serverLocation 'serverNameHere' -username 'userNameHere' -configPath 'configPathHere' 

It correctly grabs all the other parameters I pass into the first script. It just won't pass the configPath to the second script in the 'filepath'.

How can I get this parameter to pass through? Thanks so much.


Solution

  • In script2.ps1, you should define a param() block that accepts the parameter, and then you can refer to it by whatever name you give it. If you don't want to use a param() block then using $args[0] should work.