0

I'm trying to cleanup my script and I have ran into this issue.

When I run

Invoke-Command -ComputerName $Computer -ScriptBlock {Start-Process "c:\temp\openVPN\openvpn-install-2.4.8-I602-Win7.exe"

I get no issues but when I set a variable so that script looks like this,

$filepath = "c:\temp\openVPN\openvpn-install-2.4.8-I602-Win7.exe"
Invoke-Command -ComputerName $Computer -ScriptBlock {Start-Process -FilePath $filepath }

I receive the error

Cannot validate argument on parameter 'FilePath'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

Thanks!

  • 1
    You can use the `$using` scope modifier here: `$using:filepath`. This will allows variables from the calling scope to be accessible in the remote scope. – AdminOfThings Jan 28 '20 at 22:02

1 Answers1

2

Your Invoke-Command is executed on a remote computer. That runs in a different scope than the calling scope (your local session) and therefore has no access to local variables. The $using scope modifier allows variables defined in the current scope to be accessible in the remote scope.

Invoke-Command -ComputerName $Computer -ScriptBlock {Start-Process -FilePath $using:filepath }

Note that this assumes the file path does exist on the remote computer. See About Scopes for additional information.

AdminOfThings
  • 23,946
  • 4
  • 17
  • 27