Link to home
Start Free TrialLog in
Avatar of winsystems
winsystems

asked on

Exchange PowerShell Do While not working as expected

This is a small part of a larger script

I am searching for an existing SMTP address and if it is found, then adding a "incremented" number as a prefix to the address and searching again.  When I find an address that doesn't exist I wish to use this "New" address further in the script.  

Here is the code I am having issues with:

# This portion of the Script starts with a 'valid' Mail Contact Alias  
$MailContactAlias = "JohnDoe@Contoso.com"
$NewMailContactSmtpAddress = (Get-MailContact $MailContactAlias).ExternalEmailAddress.SmtpAddress
$Val = 0
Do
{
    $Val++
    $Prefix = ($Val -as [string])
    $NewMailContactSmtpAddress = $Prefix + $MailContactAlias
    $MailContactAddress = Get-Recipient $NewMailContactSmtpAddress -ErrorAction SilentlyContinue
    # Pause inserted for testing
    Pause
    if ($MailContactAddress -eq $Null)
        {
        $MailContact = "NotFound"
        }
    else
        {
        $MailContact = "Exist"
        }
}
While ($MailContact -eq "NotFound")

Even thought the variable $MailContact has a value of "NotFound" the Do ... While will not 'exit'.  

I am still relatively new to scripting so all information is greatly appreciated.
ASKER CERTIFIED SOLUTION
Avatar of Adam Brown
Adam Brown
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial