【问题标题】:PSExec: cmd exited on [COMPUTER] with error code -196608PSExec:cmd 在 [COMPUTER] 上退出,错误代码为 -196608
【发布时间】:2021-01-24 20:31:14
【问题描述】:

我一直在尝试使用 PSExec 在网络上运行 Powershell 脚本,但每次运行它时,都会收到以下消息:

PsExec v2.2 - Execute processes remotely
Copyright (C) 2001-2016 Mark Russinovich
Sysinternals - www.sysinternals.com

Couldn't access [COMPUTER]:
The filename, directory name, or volume label syntax is incorrect.

C:\PSTools> psexec \\[COMPUTER] /s cmd /c %SystemRoot%\system32\WindowsPowerShell\
v1.0\powershell.exe -ExecutionPolicy Bypass -file c:\apps\test.ps1

PsExec v2.2 - Execute processes remotely
Copyright (C) 2001-2016 Mark Russinovich
Sysinternals - www.sysinternals.com


The argument 'c:\apps\test.ps1' to the -File parameter does not exist. Provide
the path to an existing '.ps1' file as an argument to the -File parameter.
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

cmd exited on [COMPUTER] with error code -196608.

脚本的目标是遍历连接域的笔记本电脑并将特定的目录列表保存到我计算机上的文件中。该脚本如下:

powershell.exe -command "& {& get-childitem 'c:\users\*\appdata\local\google\chrome\user*data\default\extensions\*'}" > \\[MY_COMPUTER]\C$\users\[USERNAME]\desktop\Chrome_Extensions.txt

我用这个命令调用这个脚本:

psexec \\[COMPUTER] /s cmd /c %SystemRoot%\system32\WindowsPowerShell\
v1.0\powershell.exe -ExecutionPolicy Bypass -file c:\apps\test.ps1

当我在没有psexec \\[COMPUTER] /s 的情况下运行脚本时,它运行良好并保存有关我正在运行它的计算机的正确信息,然后将信息发送到我的计算机桌面。但是使用上述行运行它会导致上述错误,并且我无法定位网络上的机器。那么,PSExec 命令会导致这个错误吗,因为它看起来并没有很多东西呢?也许我误解了,并假设 PSExec 应该与 PowerShell 一样运行,但很可能并非如此。我想我只是对如何使用它有点迷茫,任何帮助将不胜感激!谢谢!

【问题讨论】:

  • 在我看来,文件c:\apps\test.ps1 在远程机器上不存在。文件有多大?如果它小于 ~ 4KB,您可以将其编码为 utf16-le base64,然后将整个内容传递给 -EncodedCommand。请参阅powershell.exe /help,他们在最后一个示例中展示了这样做。
  • @briantist 啊,我可能认为c:\apps\test.ps1 指的是我正在运行脚本的计算机上的文件,而不是目标计算机。我真的没有可靠的方法将该脚本放在每个人的计算机上,是否可以为此使用-c 开关?如果是这样,这些脚本的语法将如何变化?感谢您的帮助!
  • 可以使用-c,但如果这样做,您需要将脚本格式化为一行,并且您必须转义内容才能在命令行上正常工作。这就是我推荐使用-EncodedCommand 的原因,因为您可以避免所有这些混乱。您可以将脚本保存在本地,并对其内容进行编码。但是你可以通过这种方式传递的脚本有多大。
  • 如果 PowerShell 远程处理在目标计算机上可用,您也可以考虑使用它,而不是 psexec。为此,请在本地运行 powershell 并查看是否可以连接:Enter-PSSession -ComputerName <target computer>。如果可行,那么您也可以使用Invoke-Command -ComputerName <target> -FilePath <script>。那在本地读取脚本内容,然后远程执行。

标签: powershell networking cmd directory psexec


【解决方案1】:

在@Briantist 的帮助下,我已经能够在目标计算机上运行命令Enable-PSRemoting,然后使用invoke-command -computername [COMPUTER] -filepath c:\apps\test.ps1 运行脚本

使用 PSExec 太麻烦了,所以我很高兴它能够以另一种方式工作。现在要弄清楚如何在域中的所有计算机上启用远程处理,而无需单独连接到所有计算机并手动执行...

【讨论】: