【问题标题】:Can't get New-ADUser result out from Powershell无法从 Powershell 中获取 New-ADUser 结果
【发布时间】:2016-11-02 06:51:42
【问题描述】:

我正在寻找一种将结果从 New-ADUser 重定向到文件的方法

这就是我测试过的:运行以下脚本两次

New-ADUser -Name User64 |输出文件 C:\Temp\test.txt

New-ADUser -Name User64 2>&1 >> C:\Temp\test.txt

但是这些都不起作用,这些只是输出一个空文件并在 Powershell 中显示错误

New-ADUser : 指定的帐号已经存在 在 line:1 char:11

而不是在 test.txt 文件中输出。

对此有什么想法吗?

谢谢, 德里克

【问题讨论】:

  • 你想从 new-aduser cmdlet 中得到什么数据?
  • 错误“New-ADUser: The specified account already exists ....”应该重定向到test.txt文件而不是Powershell控制台
  • New-ADUser 'UserName' -ErrorVariable ErrorItem 如果你打开$ErrorItem,你会得到错误信息

标签: powershell powershell-2.0


【解决方案1】:

尝试使用 try/catch 构造,如下所示:

try
{
    New-ADUser -Name User64 -ErrorAction Stop
}
catch
{
    $Error[0].Exception.Message | Out-File C:\Temp\test.txt -Append
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    相关资源
    最近更新 更多