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 に答える