【问题标题】:sort status in script (powershell)脚本中的排序状态(powershell)
【发布时间】:2021-12-17 18:19:51
【问题描述】:

你好! 我有一个脚本可以检查 AD 中的用户,无论他们是否存在。如果它们存在,则在末尾显示“existiert”,如果不存在,则显示“existiert nicht”。 enter image description here 正如您在图片中看到的,它没有排序。我如何分类所有存在的都在一起,所有不存在的都在一起。我的脚本在这里:

$UserList = gc "C:\Users\d-phd001\Desktop\AD-Benutzer Checker - Copy\Input\Benutzer&Gruppen_Input.txt" 
$outputFilePath = "C:\Users\d-phd001\Desktop\AD-Benutzer Checker - Copy\Output\Benutzer&Gruppen_Output.csv"

$finalResult = foreach ($u in $UserList)
{
    $obj = [PSCustomObject]@{
        UserName = $u
        Status = ""
    }
    try
    {
        $ADUser = Get-ADUser -Identity $u -ErrorAction Stop
        $obj.Status = "Existiert"
    }
    catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
    {
        $obj.Status = "Existiert Nicht"
    }
    catch
    {
        $obj.Status = $_.Exception.Message
    }
    $obj
}

$finalResult | Export-Csv -Path $outputFilePath -NoTypeInformation -Force

非常感谢!!!

【问题讨论】:

  • "正如您在图片中看到的,它没有排序" - 实际上我们看不到,因为您显示的输出中的所有行都具有相同的 Status (@ 987654324@)
  • 哎呀…………

标签: powershell sorting active-directory script


【解决方案1】:

使用Sort-Object$finalResult 数组进行排序:

$finalResult |Sort-Object -Property Status |Export-Csv -Path $outputFilePath -NoTypeInformation -Force

【讨论】:

    猜你喜欢
    • 2022-11-17
    • 2013-12-14
    • 1970-01-01
    • 2022-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    相关资源
    最近更新 更多