0
import os
os.system("powershell.exe [Get-ItemProperty 
  HKLM:\\Software\\Wow6432Node\\Microsoft\\\Windows\\CurrentVersion\\Uninstall\\*| Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize > D:\\application whitelisting\\InstalledProgramsPS.txt ]")

这是我的代码。我想显示系统中安装的软件列表。

但我收到错误,例如 Select-Object 未被识别为外部或内部命令。当我使用 powershell 执行相同的命令时,它工作正常。

有人可以帮忙吗?提前致谢。

4

1 回答 1

1

原因是Powershell的命令参数构造不正确。os.system()call 将启动 CMD 会话,并且Select-Object is not recognized as an external or internal command是来自 CMD 的错误消息。

让我们看看 CMD 做了什么。首先它将运行 Powershell 并传递一些参数。请注意三重反斜杠,它本身就是一个错误,需要修复。

powershell.exe [Get-ItemProperty HKLM:\\Software\\Wow6432Node\\Microsoft\\\Windows\\CurrentVersion\\Uninstall\\*
| Select-Object DisplayName, DisplayVersion, Publisher, InstallDate

现在,第一件事是调用 Powershell 并(错误地)传递了参数化的 Get-ItemProperty。因为管道字符|也在 CMD 中使用,所以它被解释为 CMD 的命令。因此 CMD 尝试将第一个命令的输出通过管道传输到Select-Object,但 CMD 中没有这样的命令。因此错误。

要解决此问题,请使用-command "<commands>"将命令传递给 Powershell。双引号"用于创建 CMD 作为参数传递给 Powershell 的单个字符串。

powershell.exe -command "Get-ItemProperty HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\*| Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table -AutoSize > D:\\application whitelisting\\InstalledProgramsPS.txt"
于 2019-08-08T06:02:44.723 回答