Solved

Exchange PowerShell Do While not working as expected

Posted on 2016-08-01
1
60 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 40

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This article explains how to install and use the NTBackup utility that comes with Windows Server.
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 an email address policy 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 Mail Flow…
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

739 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