Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用python执行powershell命令_Python 3.x_Powershell - Fatal编程技术网

Python 3.x 使用python执行powershell命令

Python 3.x 使用python执行powershell命令,python-3.x,powershell,Python 3.x,Powershell,这是我的密码。我想显示系统中安装的软件列表 但我得到的错误就像 “选择对象”未被识别为外部或内部命令。 当我使用powershell执行相同的命令时,它工作正常 有人能帮忙吗? 提前感谢。原因是Powershell的命令参数构造不正确。os.system调用将启动CMD会话,并且Select Object不被识别为外部命令,或者是来自CMD的错误消息 让我们看看CMD做什么。首先,它将运行Powershell并传递一些参数。请注意三重反斜杠,它本身就是一个错误,需要修复 import os os

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

但我得到的错误就像 “选择对象”未被识别为外部或内部命令。 当我使用powershell执行相同的命令时,它工作正常

有人能帮忙吗?
提前感谢。

原因是Powershell的命令参数构造不正确。os.system调用将启动CMD会话,并且Select Object不被识别为外部命令,或者是来自CMD的错误消息

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

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 ]")
现在,首先调用Powershell并传递错误参数化的Get-ItemProperty。由于管道字符|也用于CMD,因此它被解释为CMD的命令。因此,CMD尝试通过管道将第一个命令的输出传输到Select对象,但CMD中没有这样的命令。这就是错误

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

powershell.exe [Get-ItemProperty HKLM:\\Software\\Wow6432Node\\Microsoft\\\Windows\\CurrentVersion\\Uninstall\\*
| Select-Object DisplayName, DisplayVersion, Publisher, InstallDate
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"