1

I created a script to pull some info from AD, the problem I'm having is the Secondary SMTP address field has more then one line. I'd like to show each secondary SMTP in a new line. My Script output looks like {smtp:joe.rodriguez@con...

$searchBase = 'OU=Users,DC=Contoso,DC=LOCAL'

$users = Get-ADUser -filter 'enabled -eq $true' -SearchBase $searchBase |select -expand samaccountname

Foreach ($user in $users){ 
$Secondary = get-recipient -Identity $user -ErrorAction SilentlyContinue| select Name -ExpandProperty emailaddresses |? {$_.Prefix -like "SMTP" -and $_.IsPrimaryAddress -like "False"} |select -ExpandProperty $_.Smtpaddress 

New-Object -TypeName PSCustomObject -Property @{
Name = Get-ADUser -Identity $user -Properties DisplayName |select  -ExpandProperty DisplayName
"Login ID" = Get-ADUser -Identity $user -Properties SamAccountName |select -ExpandProperty SamAccountName
Primary = get-recipient -Identity $user -ErrorAction SilentlyContinue| select Name -ExpandProperty emailaddresses |? {$_.Prefix -like "SMTP" -and $_.IsPrimaryAddress -like "True"} |select -ExpandProperty Smtpaddress 
Secondary =  $Secondary 
  }
}
4
  • When you populate $Secondary, I think you want to use select -ExpandProperty smtpaddress instead of $_.smtpaddress to get the secondary email address(es). Commented Mar 20, 2014 at 18:38
  • That then shows {[email protected]... there list is still not expanded Commented Mar 20, 2014 at 18:45
  • No, but beforehand $Secondary was being populated with all selected properties (Name, ProxyAddressString, etc.) rather than just the email address(es). It's a step in the right direction. Commented Mar 20, 2014 at 18:49
  • 2
    The default display formatting won't put those addresses on separate lines. You'll have to write a script or function to display them that way, or create a custom object type and define a custom display format in types.ps1xml. Commented Mar 20, 2014 at 18:55

1 Answer 1

2

Personally I'd make an array, pull your user list, and then iterate through the secondary SMTP addresses for each user adding your custom object to the array for each entry.

$Userlist = @()

$searchBase = 'OU=Users,DC=Contoso,DC=LOCAL'
$users = Get-ADUser -filter 'enabled -eq $true' -SearchBase $searchBase -Properties DisplayName

Foreach ($user in $users){ 
    $Recip = get-recipient -Identity $user.samaccountname -ErrorAction SilentlyContinue| select Name -ExpandProperty emailaddresses |? {$_.Prefix -like "SMTP"}

    $Recip|? {$_.IsPrimaryAddress -like "False"} |select -ExpandProperty Smtpaddress |%{
        $UserList += New-Object -TypeName PSCustomObject -Property @{
            Name = $User.DisplayName
            "Login ID" = $User.SamAccountName
            Primary = $Recip|? {$_.IsPrimaryAddress -like "True"} |select -ExpandProperty Smtpaddress 
            Secondary =  $_
        }
    }
}

This script (based off your script above) also reduces the number of server queries by 3 per user I think, so it should run a ton faster.

Sign up to request clarification or add additional context in comments.

1 Comment

Sweet thats just what I was looking for!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.