Solved

Exchange PowerShell Do While not working as expected

Posted on 2016-08-01
1
54 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 39

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
In this video we show how to create a Shared Mailbox 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 >> Sha…
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…

770 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