Exchange SMTP Splitting at the "@"

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 3 years, 8 months ago.

  • Author
    Posts
  • #9125
    Profile photo of Jason Klingensmith
    Jason Klingensmith
    Participant

    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

  • #9129
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

  • #9130
    Profile photo of Jason Klingensmith
    Jason Klingensmith
    Participant

    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

  • #9136
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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 $_.

  • #9137
    Profile photo of Jason Klingensmith
    Jason Klingensmith
    Participant

    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.

  • #9138
    Profile photo of Don Jones
    Don Jones
    Keymaster
  • #9205
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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 $_
        }
    }

You must be logged in to reply to this topic.