简体   繁体   中英

Get-ADUser Check for conflicting proxyAddresses

Currently I have a script that creates user accounts.

Note: Not all users have the same UPN (UserPrincipalName)

User accounts are in the following format: <firstinit><lastname> .

If this conflicts, the format will be changed to: <firstinit><middleinit><lastname>

Recently I have ran into an issue where the user's proxyAddress is conflicting with existing users. This is a problem because AD will not catch this.

Issue:

Checking every AD-User 's proxy address is very time consuming if not included in the filter. However, when including proxyAddresses in the filter the results are inconsistent. I am assuming this is because the proxyAddresses attribute is an array.

Inconsistent :

Import-Module ActiveDirectory
$FirstLast = "jrider@ChuckNorrisKills.com"

$conflictCheck = Get-ADUser -Properties mail, proxyAddresses -Filter "mail -eq '$FirstLast' -or UserPrincipalName -eq '$FirstLast' -or proxyAddresses -eq `"smtp:'$FirstLast'`"" | measure
if($conflictCheck.Count -gt 0)
{
    Write-Host "New user conflicts with existing user" -ForegroundColor Red 
}

I have come up with a solution that will resolve me issue. Unfortunately this is very slow (expected):

Import-Module ActiveDirectory
function Test-NewADUser
{    
    Param(
        [Parameter(Mandatory=$true)][string]$firstname, 
        [Parameter(Mandatory=$true)][string]$lastname,         
        [Parameter(Mandatory=$false)][string]$middle        
    )    
    [bool]$proxExsists = $false

    $domain = '@chuckNorrisKills.com'    
    $FirstLast = $firstname.Substring(0,1)+$lastname+$domain
    Get-ADUser -Filter * -Properties proxyAddresses | foreach {
            #xpand the proxy address and iterate through it
            foreach($address in $_.proxyAddresses)
            {
                #As you can see this goes through every user
                Write-Host "Address: " $address -ForegroundColor Yellow                
                if($address -eq "smtp:$FirstLast")
                {
                    Write-Host "Found Conflict" -ForegroundColor Red
                    $proxExsists = $true
                }
            }            
        }   
}

Test-NewADUser -firstname jack -lastname Rider

Question(s):

  1. Is there a way to expand proxyAddresses and check for conflicts in the -Filter ?
  2. If not, should I bother with Jobs, or an alternate way of checking for conflicts?

Thank you in advance for any help

You don't need to expand it, as the proxyAddress filter should be reliable.

So, this should be very straightforward:

function Validate-proxyAddress($email)
{

    if (Get-ADUser -Filter "proxyAddresses -eq 'smtp:$email'")
    {
        return $true
    }
    elseif (Get-ADUser -Filter "mail -eq '$email'")
    {
        return $true
    }
    elseif (Get-ADUser -Filter "UserPrincipalName -eq '$email'")
    {
        return $true
    }

    return $false
}

or you can join it all in one like your code, hasn't tested it, so if you get false, the user not exist, should be ok to continue...

Also, you can use -like instead of -eq if you need (in cases where missing the smtp prefix somehow):

"proxyAddresses -like '*$email*'"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM