[英]Why can't I execute remote scripts using powershell (5.1)
I have two computers on the same domain.我在同一个域上有两台计算机。 winrm is running on both machines. winrm 在两台机器上运行。 Test-WSMan is running on the remote machine. Test-WSMan 正在远程机器上运行。 I can execute invoke-command -scriptblock {dir c:\automatedtests} -computername RemoteComputername
and it returns the expected result:我可以执行invoke-command -scriptblock {dir c:\automatedtests} -computername RemoteComputername
并返回预期结果:
Directory: C:\automatedtests
Mode LastWriteTime Length Name PSComputerName
d----- 1/11/2022 5:45 PM ErrorLog.txt us14-pacci04
a----- 1/13/2022 12:16 PM 18 test.ps1 us14-pacci04
. . But if I try to execute a script on that same machine ( invoke-command -computername 'RemoteComputerName' -filepath 'C:\AutomatedTests\test.ps1'
) I get the error:但是,如果我尝试在同一台机器上执行脚本( invoke-command -computername 'RemoteComputerName' -filepath 'C:\AutomatedTests\test.ps1'
)我会收到错误消息:
invoke-command : Cannot find path 'C:\AutomatedTests\test.ps1' because it does not exist.
At line:1 char:1
+ invoke-command -session $s -filepath {C:\AutomatedTests\test.ps1}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\AutomatedTests\test.ps1:String) [Invoke-Command], ItemNotFoundExcepion
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand
I have been googling this for days with no solution.这几天我一直在谷歌上搜索,但没有解决方案。 What am I missing?我错过了什么? It's got to be something simple that I'm missing.我想念的东西一定很简单。
Edit 1/18/22 1:18pm PT: OK - I understand now that -filepath refers to a local file.编辑 1/18/22 下午 1:18 PT:好的 - 我现在明白 -filepath 是指本地文件。 So the only way to run a script on a remote computer (with the script stored there) is to use the share?那么在远程计算机上运行脚本(脚本存储在那里)的唯一方法是使用共享?
You can use $sess = New-PSSession...
to connect to the remote machine, then Copy-Item -ToSession $sess...
to copy the file over to a folder on the remote machine, then Invoke-Command -Session $sess...
to run it on the remote machine.您可以使用$sess = New-PSSession...
连接到远程计算机,然后Copy-Item -ToSession $sess...
将文件复制到远程计算机上的文件夹,然后Invoke-Command -Session $sess...
在远程机器上运行它。
(This saves you the double-hop problem of running a command on the remote machine which needs to connect out to a fileshare) (这可以避免在需要连接到文件共享的远程机器上运行命令的双跳问题)
You can invoke the script on the remote host using the call operator &
:您可以使用调用运算符&
调用远程主机上的脚本:
Invoke-Command -ScriptBlock { & 'c:\automatedtests\test.ps1'} -ComputerName RemoteComputer
Or the dot sourcing operator .
或dot sourcing 运营商.
: :
Invoke-Command -ScriptBlock { . 'c:\automatedtests\test.ps1'} -ComputerName RemoteComputer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.