Author Posts

August 6, 2013 at 12:44 pm

I am trying to split our Primary SMTP addresses at the "@" so that I can set a secondary SMTP with a different ending after the "@". I'll keep out our actual domain. We currently have "@education.edu" for our email addresses, but we want to add "@ol.education.edu" in perparation for a project. I have looked at a few options, but I am not a master scripter and I keep getting either errors, or results other than desired. Any help would be appreciated.

Import-Csv c:\temp\filename.csv | %{Get-Mailbox -Identity $_.Identity | ft PrimarySMTPAddress}

That is where I was trying to start from.

Jason

August 6, 2013 at 1:04 pm

Well, stop using Format-Table. The output of that isn't usable in code.

If you did something like...

$x = Import-Csv c:\temp\filename.csv | %{Get-Mailbox -Identity $_.Identity

Then $x would contain a bunch of mailbox objects. I would probably do this in a more script-based fashion, not a one-liner. So my second line of code would be to enumerate the mailboxes.

foreach ($box in $x) { }

In those {} is where you do all the work. You're going to need to get the current address into a variable – say, $old. Then,

$pieces = $old -split '@'

After running that, $pieces[0] will contain whatever came before the @, and $pieces[1] will contain whatever came after the @. The @ itself will be gone. You can use those, hopefully, to construct your new email address.

August 6, 2013 at 1:31 pm

Thank you, this is a good start.

This is where I've gotten to so far, but I can't seem to get any output to verify that it is working.

$x=Get-Mailbox -Database ex\exsg2db
foreach ($box in $x) {$old=get-mailbox $_.Identity | Select PrimarySmtpAddress}
$pieces=$old -split '@'
$end="@ol.education.edu"
$new=$pieces[0]+$end
Write-Host $new

August 6, 2013 at 1:37 pm

So, you don't need to do:

$old=get-mailbox $_.Identity | Select PrimarySmtpAddress

Because $box is already that mailbox. You also can't use $_. And you need to put ALL the code inside the foreach loop.

$x=Get-Mailbox -Database ex\exsg2db
foreach ($box in $x) {
  $pieces = ($old.PrimarySmtpAddress) -split '@'
  $new = "$($pieces[0])@ol.education.edu"
  Write-Output $new          # we hates write-host

  # You could also just use
  # $new = $old.PrimarySmtpAddress -replace 'education.edu','ol.education.edu'
  # instead of everything else in currently the foreach loop
}

What you were doing before wasn't working because you had it all outside the ForEach loop. Read "about_foreach" in the shell for more information on that construct; it is NOT the same as the ForEach-Object cmdlet and does not support $_.

August 6, 2013 at 1:58 pm

Thank you so much. This has me started on the right path. I'm learning the scripting "under fire" and on my own. I appreciate all the help. I need to find some good resources to learn more before it gets too far past my abilities.

August 8, 2013 at 4:30 am

It's probably not necessary in this case, since Exchange should always be giving you well-formatted email addresses, but if you were accepting some string input that needs validation (and separation), I'd recommend just using the .NET System.Net.Mail.MailAddress class. If you try to cast a string to that type and it's formatted wrong (multiple @ symbols, illegal characters, etc), you'll get an exception. Once you have the object, it already provides "User" and "Host" properties you can read:

$maybeValidEmailAddresses = @('user@domain.com','bogus@string@invalid.com')

foreach ($string in $maybeValidEmailAddresses) {
    try {
        $addr = [System.Net.Mail.MailAddress]$string
        Write-Host "User: $($addr.User), Host: $($addr.Host)"
    } catch {
        # Handle the error however you'd like.
        Write-Error -ErrorRecord $_
    }
}