【发布时间】:2019-10-09 07:14:09
【问题描述】:
从 Python 脚本 (subprocess.Popen) 调用 Powershell 时,我正在循环浏览 AD 域控制器列表。对于每个无法识别 AD 对象的控制器,我想抑制错误输出。
在 Powershell 命令末尾使用| Out-Null 无效。
Python 脚本:
for server in ADDomainList:
cmd = 'powershell.exe get-ADComputer ' + hname + ' -Server ' + server + ' | Out-Null'
subprocess.call(cmd)
从 Powershell 命令行:
get-ADComputer computer-name -Server server.domain.com
不需要的输出:
Get-ADComputer : A positional parameter cannot be found that accepts argument '?'.
At line:1 char:1
+ get-ADComputer computer-name -Server server.domain.com ?
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ADComputer], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
返回码为 0 或 1 的结果是我在接下来的执行步骤中需要捕获的全部内容。我不希望控制台有任何输出。
【问题讨论】:
-
将输入验证代码添加到您的 python 代码或您的 PoSh 脚本中。
标签: powershell command output line