获取 ADPropertyValueCollection 的长度,或检查是否设置了 empty/not
Get length of ADPropertyValueCollection, or check if empty/not set
我正在遍历 AD 用户列表并报告他们的 proxyAddresses。我希望能够检查长度 0 或检查 null/not 集。我使用 GetType 函数找出 proxyAddresses 属性的类型是 ADPropertyValueCollection。但是查看文档( https://docs.microsoft.com/en-us/dotnet/api/microsoft.activedirectory.management.adpropertyvaluecollection-1?view=activedirectory-management-10.0 )我找不到任何方法来获取长度或检查它是否 null/empty.
这是我当前的代码,我只是检查它是否有内置的 PowerShell $null 变量,但是当没有为用户设置 proxyAddresses 属性时它不会捕获:
$users = Get-ADUser -Filter * -SearchBase 'DC=Domain,DC=local' -Properties "proxyAddresses"
$outputStr = ''
foreach ($user in $users) {
if ($user.DistinguishedName -inotmatch "OU1|OU2|OU3") {
# Skips users not in OU's we care about
continue
}
if ($user.proxyAddresses -eq $null) {
# This never fires, despite users existing with the attribute not set
Write-Output "WARNING: User $($user.Name) found with proxyAddresses not set."
}
$outputStr += "Proxy addresses for $($user.Name)`r`n"
foreach ($addr in $user.proxyAddresses) {
$outputStr += "$($addr)`r`n"
}
$outputStr += "`r`n"
}
Out-File -FilePath 'c:\data\aliases.txt' -Encoding utf8 -InputObject $outputStr
这很有趣,如果他们没有设置代理地址,似乎 属性 不会与用户对象一起返回。您应该能够像这样测试代理地址的存在。
if (!$user.proxyAddresses) {
Write-Host "WARNING: User $($user.Name) found with proxyAddresses not set."
}
我正在遍历 AD 用户列表并报告他们的 proxyAddresses。我希望能够检查长度 0 或检查 null/not 集。我使用 GetType 函数找出 proxyAddresses 属性的类型是 ADPropertyValueCollection。但是查看文档( https://docs.microsoft.com/en-us/dotnet/api/microsoft.activedirectory.management.adpropertyvaluecollection-1?view=activedirectory-management-10.0 )我找不到任何方法来获取长度或检查它是否 null/empty.
这是我当前的代码,我只是检查它是否有内置的 PowerShell $null 变量,但是当没有为用户设置 proxyAddresses 属性时它不会捕获:
$users = Get-ADUser -Filter * -SearchBase 'DC=Domain,DC=local' -Properties "proxyAddresses"
$outputStr = ''
foreach ($user in $users) {
if ($user.DistinguishedName -inotmatch "OU1|OU2|OU3") {
# Skips users not in OU's we care about
continue
}
if ($user.proxyAddresses -eq $null) {
# This never fires, despite users existing with the attribute not set
Write-Output "WARNING: User $($user.Name) found with proxyAddresses not set."
}
$outputStr += "Proxy addresses for $($user.Name)`r`n"
foreach ($addr in $user.proxyAddresses) {
$outputStr += "$($addr)`r`n"
}
$outputStr += "`r`n"
}
Out-File -FilePath 'c:\data\aliases.txt' -Encoding utf8 -InputObject $outputStr
这很有趣,如果他们没有设置代理地址,似乎 属性 不会与用户对象一起返回。您应该能够像这样测试代理地址的存在。
if (!$user.proxyAddresses) {
Write-Host "WARNING: User $($user.Name) found with proxyAddresses not set."
}