0

我创建了一个脚本来从 AD 中提取一些信息,我遇到的问题是辅助 SMTP 地址字段多于一行。我想在新行中显示每个辅助 SMTP。我的脚本输出看起来像{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

1 回答 1

1

我个人会创建一个数组,拉出您的用户列表,然后遍历每个用户的辅助 SMTP 地址,将您的自定义对象添加到每个条目的数组中。

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

我认为这个脚本(基于上面的脚本)还将每个用户的服务器查询数量减少了 3 个,因此它应该运行得更快。

于 2014-03-20T19:59:22.063 回答