Powershell can't read my variable as start-process file path [duplicate]

拜拜、爱过 提交于 2021-01-29 10:12:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!