2

I am trying to write a PowerShell script that will cycle through the ProxyAddresses for all of my AD user objects. If a user has a SMTP address with [email protected], I want it to check if they also have a matching SMTP address of [email protected] and if not then add it, etc.

$ADobjects = @(Get-ADObject -Filter 'objectClass -eq "User"' -Properties mailNickname,ProxyAddresses -SearchBase "OU=Test,DC=domain,DC=local")

$TempArr = @()
$OldDomain = "@domain.local"
$NewDomain = "@domain.com"

$ADobjects | ForEach-Object { ## Cycle thru each AD object
    $PrimaryProxyAddress = $_.mailNickname+$NewDomain
    $TempStr = ""
    $TempAdd = ""
    If ($ADobjects.Count -ge 1) ## Make sure there is at least one item to work on
        {
        $TempArr = $_.ProxyAddresses ## Set $TempArr so that it contains all of the proxy addresses
        $TempArr | ForEach-Object { ## Cycle thru each proxy address of each AD object
        If ($_.Contains($OldDomain) -eq "True") ## Does the proxy address contain the old domain?
            { ## Come here if the proxy address contains the old domain
            $TempStr = $_ -replace $OldDomain, $NewDomain ## Replace the $OldDomain name with $NewDomain
            If ($TempArr.Contains($TempStr) -ne "True") ## See if we already have an address with the new domain name
                {
                write-host $TempStr
                $TempAdd = $TempAdd + " " + $TempStr ## We don't have one so add it to the list of SMTP addresses to add
                ## I've removed all of the addition stuff to keep the script shorter
                }
            }
        }
        }
}

It works until I get to the part

If ($TempArr.Contains($TempStr) -ne "True")

The $TempArr array may be something like

“SMTP:[email protected] smtp:[email protected] smtp:[email protected] smtp:[email protected] x400 etc”

The $TempStr may be something like

“SMTP:[email protected]

My $TempStr does exist in the $TempArr array but my IF statement never returns TRUE (so it always thinks that the IF statement is –ne TRUE).

Isn’t CONTAINS supposed to be case insensitive by default in PowerShell? If it is case insensitive, doesn’t “SMTP:[email protected]” -eq “smtp:[email protected]”? Or is it maybe a data-type issue (I don’t get any errors) since one is an array and the other is a string? What am I missing here?

Thanks much

1 Answer 1

4

Isn’t CONTAINS supposed to be case insensitive by default in PowerShell?

The -contains operator is case insensitive.

The .Contains() method is case sensitive.

If you want to use the Contains() method, convert both strings to one case before compare. Something like:

If ($TempArr.ToLower().Contains($TempStr.ToLower()) -ne "True")
2
  • 1
    Great answer - unfortunately I got the following error: // Method invocation failed because [Microsoft.ActiveDirectory.Management.ADPropertyValueCollection] doesn't contain a method named 'ToLower' // But you gave me the info that I needed and I changed the line from a method to an operator and now it works // If ($TempArr -Contains $TempStr -ne "True") // Thank you!!
    – SOSidb
    Feb 22, 2014 at 0:09
  • Yeah I was wondering about that. ToLower is for strings, not arrays -- but hey, you figured it out, that's what counts. ;) Feb 22, 2014 at 0:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .