Solved

Exchange PowerShell Do While not working as expected

Posted on 2016-08-01
1
47 Views
Last Modified: 2016-08-01
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.
0
Comment
Question by:winsystems
1 Comment
 
LVL 38

Accepted Solution

by:
Adam Brown earned 500 total points
ID: 41737911
Two things...you'll want to define $mailcontact before the loop. You are also telling your script to loop as long as $mailcontact equals "NotFound", so unless you made a typo on this:

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

It's running the way you coded it. If that's what you want, you'll need to put a ! in front of ($MailContact -eq "NotFound") to make it a NOT action. I can't remember if that also has to be in parentesis. I think it does, so like this:

While (!($MailContact -eq "NotFound"))

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Exchange server is not supported in any cloud-hosted platform (other than Azure with Azure Premium Storage).
In this video we show how to create a Distribution Group in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >>…
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now