首页 > 解决方案 > 脚本中的排序状态(powershell)

问题描述

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

$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

非常感谢!!!

标签: powershellsortingactive-directoryscript

解决方案


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

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

推荐阅读