在远程 PC 上运行 PowerShell 脚本

发布于 2024-08-12 01:47:03 字数 886 浏览 5 评论 0原文

我已在远程 PC(RPC001)上安装了 PS 1.0。我使用 Windows Sysinternals 工具 PSExec.exe 在远程执行以下进程:

 PSExec \\RPC001 -u myID -p myPWD PowerShell C:\script\StartPS.ps1 par1 par2

我可以看到 PowerShell.exe 进程随后在远程 PC 上运行,但它实际上什么也没做,只是挂在那里。我尝试在脚本中添加一个简单的“Write-Output/Host”代码。我通过 RTS 在远程运行相同的脚本,它在那里工作。

不确定我是否错过了使用 PSExec 运行脚本的其他任何内容,或者是 PSExec.exe 的限制。我想在远程启动 PS 脚本,以便从我的盒子中在本地执行某些操作(在本地压缩一些文件并删除旧文件)。

我在 Stackoverflow 中提出了类似的问题:通过 powershell 运行远程进程Don 建议我使用 PSExec。这听起来像是解决问题的另一种方法。但是,我无法让它与 PowerShell 一起使用。有什么方法可以让 PS 在远程 PC 上工作吗?

顺便说一句,我无法使用 PS 2.0,因为我的网络不允许我安装 PS 2.0 所需的 Windows XP SP3。

I have installed PS 1.0 on a remote PC(RPC001). I used Windows Sysinternals
tool PSExec.exe
to execute the following process on the remote:

 PSExec \\RPC001 -u myID -p myPWD PowerShell C:\script\StartPS.ps1 par1 par2

I can see the PowerShell.exe process running on the remote PC afterwards, but it is actually doing nothing, just hanging there. I tried to put a simple code of "Write-Output/Host" a string in the script. I run the same script on the remote by RTS, it works there.

Not sure if I miss anything else to run the script by using PSExec, or it is PSExec.exe limitation. I would like to start a PS script on remote to do something there locally (compress some files locally and remove old files) from my box.

I asked a similar question in Stackoverflow: Run remote process by powershell. Don suggested me to use PSExec. It sounds like an alternative way to solve the issue. However, I cannot get it working with PowerShell. Any way to get PS working on remote PC?

By the way, I cannot use PS 2.0 since my network does not allow me to install Windows XP SP3, which is required for PS 2.0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷雾森÷林ヴ 2024-08-19 01:47:03

接受的答案对我不起作用,但以下方法有效:

>PsExec.exe \\<SERVER FQDN> -u <DOMAIN\USER> -p <PASSWORD> /accepteula cmd 
    /c "powershell -noninteractive -command gci c:\"

此处的示例

The accepted answer didn't work for me but the following did:

>PsExec.exe \\<SERVER FQDN> -u <DOMAIN\USER> -p <PASSWORD> /accepteula cmd 
    /c "powershell -noninteractive -command gci c:\"

Example from here

可爱咩 2024-08-19 01:47:03

在进一步研究 PSExec 工具后,我想我得到了答案。我需要添加 -i 选项来告诉 PSExec 以交互模式在远程启动进程:

PSExec \\RPC001 -i -u myID -p myPWD PowerShell C:\script\StartPS.ps1 par1 par2

如果没有 -i,powershell.exe 将以等待模式在远程运行。有趣的一点是,如果我运行一个简单的bat(bat中没有PS),它工作得很好。也许这是 PS 外壳的特殊之处?欢迎评论和解释。

After further investigating on PSExec tool, I think I got the answer. I need to add -i option to tell PSExec to launch process on remote in interactive mode:

PSExec \\RPC001 -i -u myID -p myPWD PowerShell C:\script\StartPS.ps1 par1 par2

Without -i, powershell.exe is running on the remote in waiting mode. Interesting point is that if I run a simple bat (without PS in bat), it works fine. Maybe this is something special for PS case? Welcome comments and explanations.

ゝ杯具 2024-08-19 01:47:03

你可以尝试以下方法吗?

psexec \\server cmd /c "echo . | powershell script.ps1"

Can you try the following?

psexec \\server cmd /c "echo . | powershell script.ps1"
各自安好 2024-08-19 01:47:03

接受的答案对我不起作用,但这确实有效。确保脚本位于每个远程服务器上的以下位置(c:\temp_)。servers.txt 包含 IP 地址列表(每行一个)。

psexec @servers.txt -u <username> cmd /c "powershell -noninteractive -file C:\temp\script.ps1"

Accepted answer doesn't work for me, but this does. Ensure script in the location (c:\temp_ below on each remote server. servers.txt contains a list of IP addresses (one per line).

psexec @servers.txt -u <username> cmd /c "powershell -noninteractive -file C:\temp\script.ps1"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文