28

I was trying to execute a script in remote computer.

I did "Enable-PSremoting" in the remote machine.

I placed a script hello.ps1 in remote machine.

[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"

I got the following error.

Invoke-Command : Cannot find path 'C:\hello.ps1' because it does not exist.

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.

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. I misunderstood by seeing the return values that it is executing at client side.

4

3 回答 3

16

当你写:

invoke-command -computer $MachineName -filepath "C:\hello.ps1"

该脚本C:\hello.ps1将从客户端计算机获取并带到服务器执行。因此,您得到错误文件不存在,因为 Invoke-Command 正在客户端计算机中查找该文件。

于 2012-05-04T09:35:50.427 回答
2

我得到了同样的错误,但我在你的代码中的一个变量中挂钩了一个远程会话,最后得到了一些有用的东西:

$s = New-PSSession -ComputerName "WTxxxxxL32" -Credential $credential

Invoke-Command -Session $s -Command {D:\ServerDLLDev\RemoteCOMInstall.ps1}

有一百万个调用等解决方案,但最简单的最终对我有用。谢谢你。

于 2017-05-05T22:17:34.710 回答
-1

没关系,只是改变顺序

代替

invoke-command -computer $MachineName -filepath "C:\hello.ps1"

利用

invoke-command -filepath "C:\hello.ps1" -computer $MachineName

于 2021-12-29T14:17:14.797 回答