【问题标题】:PowerShell: cannot pipe output to file in combination with -Outputformat XML and -FilePowerShell:无法结合 -Outputformat XML 和 -File 将输出通过管道传输到文件
【发布时间】:2016-06-01 10:32:11
【问题描述】:

脚本文件c:/chgpwd.ps1

Param(
    [parameter(Mandatory=$true)]
    [string] $user,
    [parameter(Mandatory=$true)]
    [string] $oldPass,
    [parameter(Mandatory=$true)]
    [string] $newPass
)
write-host "before"
$Computername = $env:COMPUTERNAME
([adsi]"WinNT://$Computername/$user").ChangePassword("$oldPass", "$newPass")
write-host "after"

当通过

执行时
powershell -OutputFormat XML -ExecutionPolicy bypass -File c:\chgpwd.ps1 'myuser' 'a' 'b' 2>&1 > C:\out.xml

注意:用户存在但其新密码(b)与密码策略不匹配

输出文件 (C:\out.xml) 将不包含任何 xml,但 xml 将显示在控制台输出中。

目前我只获取内容(out.xml)

powershell:使用“2”参数调用“ChangePassword”的异常:“密码不符合密码策略 要求。检查最小密码长度、密码复杂性和密码历史要求。 " 在行:1 字符:1 + powershell -OutputFormat XML -ExecutionPolicy 绕过 -File C:\chgpwd.ps1 ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo> > : NotSpecified: (:) [chgpwd.ps1], MethodInvocationException + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI,chgpwd.ps1.ps1

有没有办法在文件中获取 xml 输出?

【问题讨论】:

  • 你期望什么输出?为什么需要 xml?
  • 我本来希望渲染到控制台的 xml 出现在输出文件中(如果通过管道传输)。对于解析错误消息xml会更好。

标签: xml powershell output command-line-interface


【解决方案1】:

我之前没用过-OutputFormat开关,不过你可以用export-clixml

try {
    1/0
} catch {
    $_ | Export-Clixml C:\temp\text.xml
    start C:\temp\text.xml
}

【讨论】:

  • 感谢您的快速回答:我试过了,它工作正常,但如果脚本中出现语法错误,这将无济于事,或者缺少必需的参数或找不到文件。特别是在我的情况下,脚本来自第三方。
猜你喜欢
  • 2019-01-24
  • 2015-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多