Search code examples
powershellpowershell-remoting

Trying to Invoke a Script on a Remote Computer


I'm having to construct some paths before attempting to invoke the script which is present on the remote computer. I'm adapting something we have working in TeamCity, but I'm currently getting the following error:

The command cannot be run because the File parameter requires a file path. Supply a path for the File parameter and then try the command again.
    + CategoryInfo          : NotSpecified: (The command can... command again.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Relevant code below:

$stageScript = "D:\pkg\${dir}\stage.ps1"
$options = "production \\192.168.0.x\staging \\${server}\staging"

Invoke-Command -computername server.domain.com { powershell.exe -noprofile -executionpolicy Bypass -file $stageScript $options } -Credential $credential

Solution

  • You need to pass local variables to the script block as an argument list

    $stageScript = "D:\pkg\${dir}\stage.ps1"
    $options = "production \\192.168.0.x\staging \\${server}\staging"
    
    Invoke-Command -computername server.domain.com { powershell.exe -noprofile -executionpolicy Bypass -file $args[0] $args[1] } -ArgumentList $stageScript,$options  -Credential $credential