如何执行远程机器中可用的powershell脚本? [英] How to execute a powershell script available in remote machine?
问题描述
我试图在远程计算机上执行脚本.
I was trying to execute a script in remote computer.
我在远程机器上做了Enable-PSremoting
".
I did "Enable-PSremoting
" in the remote machine.
我在远程机器上放置了一个脚本 hello.ps1
.
I placed a script hello.ps1
in remote machine.
[我的客户端机器是Windows XP,远程计算机是Windows 2003]
[My client machine is Windows XP and remote computer is Windows 2003 ]
然后我试图从我的客户端计算机上执行脚本.
Then from my client computer i was trying to execute the script.
invoke-command -computer $MachineName -filepath "C:\hello.ps1"
我收到以下错误.
Invoke-Command : 找不到路径C:\hello.ps1",因为它没有存在.
Invoke-Command : Cannot find path 'C:\hello.ps1' because it does not exist.
我认为它试图从客户端机器上查找脚本.
I think it tries to find script from client machine.
如果我尝试运行
invoke-command -computer $MachineName -command { C:\hello.ps1 }
,它在客户端执行远程机器上可用的脚本.
invoke-command -computer $MachineName -command { C:\hello.ps1 }
, It executes the script available in remote machine in client side.
但我想在远程机器上执行远程脚本.
But i would like to execute the remote script in remote machine itself.
如何让它运行远程机器上可用的脚本?
How to make it to run the script available in remote machine?
更新:
实际上这个命令 "invoke-command -computer $MachineName -command { C:\hello.ps1 }
" 在远程端工作并将结果返回给客户端.看到它在客户端执行的返回值,我误解了.
Actually this command "invoke-command -computer $MachineName -command { C:\hello.ps1 }
" works in remote side and returned the result to client side. I misunderstood by seeing the return values that it is executing at client side.
推荐答案
当你写:
invoke-command -computer $MachineName -filepath "C:\hello.ps1"
脚本 C:\hello.ps1
将从客户端计算机中取出并带到服务器执行.因此,您收到错误 file does not exist 因为 Invoke-Command 正在客户端计算机中查找该文件.
The script C:\hello.ps1
will be taken from the client computer and brought to the server to be executed. So you've got the error file does not exist because Invoke-Command is looking for the file in the client computer.
这篇关于如何执行远程机器中可用的powershell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!