Pergunta

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 
  }
}
Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top