Solved

Exchange PowerShell Do While not working as expected

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

We are happy to announce a brand new addition to our line of acclaimed email signature management products – CodeTwo Email Signatures for Office 365.
Follow this checklist to learn more about the 15 things you should never include in an email signature from personal quotes, animated gifs and out-of-date marketing content.
In this video we show how to create a mailbox database 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 Servers >> Data…
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…

910 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

20 Experts available now in Live!

Get 1:1 Help Now