[英]How to execute a powershell script available in remote machine?
I was trying to execute a script in remote computer.我试图在远程计算机上执行脚本。
I did " Enable-PSremoting
" in the remote machine.我在远程机器上做了“
Enable-PSremoting
”。
I placed a script hello.ps1
in remote machine.我在远程机器上放置了一个脚本
hello.ps1
。
[My client machine is Windows XP and remote computer is Windows 2003 ] [我的客户端机器是Windows XP,远程计算机是Windows 2003]
Then from my client computer i was trying to execute the script.然后从我的客户端计算机我试图执行脚本。
invoke-command -computer $MachineName -filepath "C:\hello.ps1"
I got the following error.我收到以下错误。
Invoke-Command : Cannot find path 'C:\\hello.ps1' because it does not exist.
调用命令:找不到路径“C:\\hello.ps1”,因为它不存在。
I think it tries to find script from client machine.我认为它试图从客户端机器上查找脚本。
If i try to run如果我试着跑
invoke-command -computer $MachineName -command { C:\\hello.ps1 }
, It executes the script available in remote machine in client side. invoke-command -computer $MachineName -command { C:\\hello.ps1 }
,它在客户端执行远程机器上可用的脚本。
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?如何让它运行远程机器上可用的脚本?
Updated:更新:
Actually this command " invoke-command -computer $MachineName -command { C:\\hello.ps1 }
" works in remote side and returned the result to client side.实际上这个命令“
invoke-command -computer $MachineName -command { C:\\hello.ps1 }
”在远程端工作并将结果返回给客户端。 I misunderstood by seeing the return values that it is executing at client side.我通过看到它在客户端执行的返回值而误解了。
When you write :当你写:
invoke-command -computer $MachineName -filepath "C:\hello.ps1"
The script C:\\hello.ps1
will be taken from the client computer and brought to the server to be executed.脚本
C:\\hello.ps1
将从客户端计算机中取出并带到服务器上执行。 So you've got the error file does not exist because Invoke-Command is looking for the file in the client computer.所以你得到的错误文件不存在,因为 Invoke-Command 正在客户端计算机中寻找该文件。
I was trying to execute a script in remote computer.我试图在远程计算机上执行脚本。
I did " Enable-PSremoting
" in the remote machine.我在远程计算机上执行了“
Enable-PSremoting
”。
I placed a script hello.ps1
in remote machine.我在远程计算机中放置了一个脚本
hello.ps1
。
[My client machine is Windows XP and remote computer is Windows 2003 ] [我的客户端计算机是Windows XP,远程计算机是Windows 2003]
Then from my client computer i was trying to execute the script.然后我从客户端计算机尝试执行脚本。
invoke-command -computer $MachineName -filepath "C:\hello.ps1"
I got the following error.我收到以下错误。
Invoke-Command : Cannot find path 'C:\\hello.ps1' because it does not exist.
调用命令:找不到路径'C:\\ hello.ps1',因为它不存在。
I think it tries to find script from client machine.我认为它尝试从客户端计算机中查找脚本。
If i try to run如果我尝试跑步
invoke-command -computer $MachineName -command { C:\\hello.ps1 }
, It executes the script available in remote machine in client side. invoke-command -computer $MachineName -command { C:\\hello.ps1 }
,它执行客户端远程计算机中可用的脚本。
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?如何使其运行在远程计算机上可用的脚本?
Updated:更新:
Actually this command " invoke-command -computer $MachineName -command { C:\\hello.ps1 }
" works in remote side and returned the result to client side.实际上,此命令“
invoke-command -computer $MachineName -command { C:\\hello.ps1 }
”在远程端有效,并将结果返回给客户端。 I misunderstood by seeing the return values that it is executing at client side.看到它在客户端执行的返回值,我误会了。
I got the same error but I hooked a Remote session in a variable in your code and finally have something that works:我遇到了同样的错误,但我在你的代码中的一个变量中连接了一个远程会话,最后得到了一些有效的东西:
$s = New-PSSession -ComputerName "WTxxxxxL32" -Credential $credential
Invoke-Command -Session $s -Command {D:\ServerDLLDev\RemoteCOMInstall.ps1}
There are a million Invoke-etc solutons but the simplest worked finally for me.有一百万个 Invoke 等解决方案,但最简单的最终对我有用。 Thanks to you.
谢谢你。
It's ok just change the order没关系,只是改变顺序
instead of代替
invoke-command -computer $MachineName -filepath "C:\\hello.ps1" invoke-command -computer $MachineName -filepath "C:\\hello.ps1"
Use利用
invoke-command -filepath "C:\\hello.ps1" -computer $MachineName invoke-command -filepath "C:\\hello.ps1" -computer $MachineName
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.