0
我想使用canonicalname而不是專有名稱,因爲在csv中排序的原因要簡潔得多。
我知道我必須把屬性值,但問題是在哪裏。我不知道它是否在$ oulist或$ users變量中。這是我的代碼。利用CanonicalName作爲辨別名稱
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
$newlist = foreach ($OU in $OUlist)
{
#The array will automatically have a count property, no need for measure
$Users = Get-ADuser -Filter * -Credential $cred -SearchBase $OU.DistinguishedName -SearchScope OneLevel -Server $domain.pdcEmulator
#Again you already have all of the user object before the loop, just write-progress for the OUlist that you are looping through
write-progress -Activity "Counting users" -Status "Finding users in $OU" -PercentComplete ([array]::indexof($OUlist, $OU)/$OUlist.count * 100)
[pscustomobject]@{
OU = $OU.DistinguishedName; Count = ($Users.Count)
}
}
if ($output -eq "Y")
{
$newlist | Export-CSV .\OUuserCount.csv -NoTypeInformation -Force
Write-Host "All done!" -ForegroundColor yellow
}
else
{
$newList | Out-GridView
}