在PowerShell中获取规范名称而不是专有名称?

时间:2017-03-14 20:38:10

标签: powershell

我想在报告中使用Canonical名称而不是专有名称。这样我可以对OU进行排序,这样可以更容易呈现。

domain.com/users/someuser/username轻松排序。

这是我的代码

Import-Module activeDirectory

$output = Read-Host "'Y' for output to file or any key for output in GUI table view" -foreground Cyan
$fqdn = Read-Host "Enter FQDN domain"
$cred = Get-Credential

Write-Host "Contacting $fqdn domain..." -ForegroundColor Yellow

$domain = (get-addomain $fqdn -Credential $cred | select distinguishedName, pdcEmulator, DNSroot, DomainControllersContainer)

Write-Host "Completed. Enumerating OUs.." -ForegroundColor Yellow

$OUlist = @(Get-ADOrganizationalUnit -filter * -Credential $cred -SearchBase $domain.distinguishedName -SearchScope Subtree -Server $domain.DNSroot)
Write-Host "Completed. Counting users..." -ForegroundColor Yellow

for ($i = 1; $i -le $oulist.Count; $i++)
    { write-progress -Activity "Collecting OUs" -Status "Finding OUs $i" -PercentComplete ($i/$OUlist.count * 100) }
$newlist = @{ }



foreach ($_objectitem in $OUlist)
{
    $getUser = Get-ADuser -Filter * -Credential $cred -SearchBase $_objectItem.DistinguishedName -SearchScope OneLevel -Server $domain.pdcEmulator | measure | select Count
    for ($i = 1; $i -le $getUser.Count; $i++)
    { write-progress -Activity "Counting users" -Status "Finding users $i in $_objectitem" -PercentComplete ($i/$getUser.count * 100) }

    $newlist.add($_objectItem.DistinguishedName, $getUser.Count)

}

if ($output -eq "Y")
{
    $newlist | ft -AutoSize | Out-File .\OUuserCount.txt


    Write-Host "All done!" -ForegroundColor yellow
}
else
{
    $newList | Out-GridView
}

1 个答案:

答案 0 :(得分:3)

我认为您只需要将此参数添加到get-aduser:

-properties canonicalname

然后将$ _objectitem.distinguishedname替换为每行$ getuser.canonicalname:

$newlist.add($getuser.CanonicalName, $getUser.Count)

您必须使用-properties,因为它不是get-aduser返回的默认属性。