问题
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:
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.
来源:https://stackoverflow.com/questions/59957695/powershell-cant-read-my-variable-as-start-process-file-path