【问题标题】:How to format multiple entries in Powershell如何在 Powershell 中格式化多个条目
【发布时间】:2013-06-05 03:39:44
【问题描述】:

我正在使用以下代码在 Powershell 中获取直接下属列表:

import-module activedirectory

$identity = read-host "Enter username:"

Get-ADUser $identity -Properties directReports | select-object {$_.directReports} | export-csv "DirectReports-$identity.csv"

这会将所有直接下属输出到一行,例如“CN=User1,CN=User2,CN=User3”.....

我如何修改它,以便将每个用户输出到自己的行?

【问题讨论】:

  • 我既没有使用也没有见过directReports 属性,但听起来你想-split on the ","

标签: powershell active-directory


【解决方案1】:

我确定这不是最干净的解决方案:

Get-ADUser $identity -Properties directReports | select-object -expandproperty directReports|foreach-object{$_.split(",")[0].replace("CN=","")}| export-csv "DirectReports-$identity.csv"

您将在 CSV 文件的单独行中获取每个人的姓名(仅)。

【讨论】:

    【解决方案2】:
    Get-ADUser $identity -Properties directReports | 
    Select-Object Name,@{n='DirectReports';e={$_.directReports.Split(',CN=',[System.StringSplitOptions]::RemoveEmptyEntries) -join ';'}} | 
    Export-Csv "DirectReports-$identity.csv"
    

    【讨论】:

    • 这似乎删除了逗号,但它们仍然输出到同一行。
    • 您不只是从 directReports 中获得名称吗?
    • 不,它将所有名称放在同一行,只是这次没有逗号。
    • 我的错,我没有注意到它包含多个 CN 项目,还以为是 DN。答案已更新。
    • 对不起,这会产生一个空白的 csv 文件
    猜你喜欢
    • 1970-01-01
    • 2022-06-18
    • 2017-02-25
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 2012-03-15
    • 2022-01-24
    相关资源
    最近更新 更多