【问题标题】:Get the canonical name instead of the distinguished name in powershell?在powershell中获取规范名称而不是专有名称?
【发布时间】:2017-06-08 00:59:27
【问题描述】:

我想在我的报告中使用规范名称而不是专有名称。这样我就可以对 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
}

【问题讨论】:

    标签: powershell


    【解决方案1】:

    我想你只需要将这个参数添加到get-aduser:

    -properties canonicalname
    

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

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

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

    【讨论】:

    • 它还是不喜欢马克。 $newlist.add($_objectItem.Canonicalname, $getUser.Count)
    • 使用“2”参数调用“添加”的异常:“键不能为空。错误:参数名称:键”usersinou.ps1 (43, 2):错误:在第 43 行字符:2 错误:+ $newlist.add($_objectItem.Canonicalname, $getUser.Count)
    • 只要我添加 $newlist.add($_objectItem.DistinguishedName, $getUser.Count, $getUser.CanonicalName) 它就会失败
    • 我那里也有-properties canonicalname
    • 使用“2”参数调用“添加”的异常:“项目已添加。字典中的键:'0' 正在添加的键:'0'” usersinou.ps1 (41, 2): 错误: At Line: 41 char: 2 ERROR: + $newlist.add($getUser.Count, $getUser.CanonicalName)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 2014-12-01
    • 1970-01-01
    相关资源
    最近更新 更多