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).Externa lEmailAddr ess.SmtpAd dress
$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.
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
$Val = 0
Do
{
$Val++
$Prefix = ($Val -as [string])
$NewMailContactSmtpAddress
$MailContactAddress = Get-Recipient $NewMailContactSmtpAddress
# 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.