简体   繁体   English

如何执行远程机器中可用的powershell脚本?

[英]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.谢谢你。

I had the exact same probe, and solved it with a combination of [WMICLASS] 's create() and Start-Process .我有完全相同的探针,并通过[WMICLASS] 's create()Start-Process的组合解决了它。

Check my answer here .在这里检查我的答案。

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.

相关问题 如何在PowerShell中远程执行ELEVATED远程脚本 - How to remote execute an ELEVATED remote script in PowerShell 如何在64位计算机上执行Powershell脚本? - How to execute powershell script in 64 bit machine? PowerShell:如何使用WMI在远程计算机上运行powershell脚本 - PowerShell: How to run a powershell script on a remote machine using WMI 在客户端计算机上执行Powershell脚本? - Execute Powershell Script on Client Machine? powershell 在远程机器上执行启动进程 - powershell execute start-process on remote machine 如何在powershell中使用invoke-command,在远程机器上运行脚本 - How to use invoke-command in powershell, to run a script on remote machine 如何通过ansible将本地powershell脚本运行到远程机器上? - How to run a local powershell script into remote machine via ansible? 如何在python中使用ssh在远程计算机上执行Powershell脚本 - how to excute powershell script on remote machine using ssh in python 如何通过Powershell从远程计算机异步运行Python脚本? - How to Run Python Script from Remote Machine via Powershell Asynchronously? 如何使用Powershell在本地计算机上执行oracle sql脚本 - how to execute a oracle sql script form local machine using powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM