User LegacyExchangeDN

administrator 

X500:/o=First Organization/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=2c5af45c0ce4497db28f174659a5e9e1-Admin

<#  
  .概要説明  
   LegacyExchangeDNをADのProxyAddresesに設定します。  
   ドメインコントローラ上でシェルを実行します。ユーザプロパティのプロキシアドレスにレコードを削除します。
  .サンプル  
   Add-UserProxyAddress -CSVFile c:\Script\Delete-LegacyExchangeDN.ps1
     
   CSVファイルから入力を受け取り、それぞれのユーザプロキシアドレス属性にX500:<LegacyExchangeDN>値を削除します。
  .サンプル
   CSV ファイルのデータフォーマット(LegacyExchangeDNの値をダブルクォーテーションで文字列を選択する。)
   ----------------------------------------------
   | user      | LegacyExchangeDN               |
   | --------------------------------------------
   | administrator  | {"X500:/o=First Organization/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recip...}  "|
   | yamada         | {"X500:/o=First Organization/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recip... }"|
   ----------------------------------------------
  .OutPuts  
   username ProxyAddresses
   -------- --------------
   
    
  #>  
   
[CmdletBinding()]
param(  
    [Parameter(Mandatory=$true,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)]
    [alias('FilePath','File','CSV','CSVPath')]
    [String]$Path,
    [String]$Protocol='X500') #param
Begin {  
    Import-Module ActiveDirectory
} #Begin

Process {
    $users = Import-Csv -Path $Path
    $temp = [System.IO.Path]::GetTempFileName()
    Foreach ($u in $users) {
        
        Try {
            $user = Get-ADUser -Identity $u.user -ErrorAction Stop
            Write-Host "$($user.SamAccountName) exists, Processing it..." -BackgroundColor DarkGray -NoNewline 
            $LegacyExchangeDN = "{1}:{0}" -f $u.LegacyExchangeDN, $Protocol
            Set-ADUser -Identity $u.user -remove @{Proxyaddresses=$u.LegacyExchangeDN} 
            Write-Host "...ProxyAddress deleted" -BackgroundColor DarkGreen
        }
        catch {
            Write-Host "$($user.SamAccountName) does not exists" -BackgroundColor DarkRed
        }
    } 
    
    $users | foreach {
        $user = $_.user
        Try {
            Set-ADUser -Identity $_.user -Properties ProxyAddresses -ErrorAction Stop | select SamAccountName, Name, ProxyAddresses
        }
        catch {
            Write-Host "$user does not exists" -BackgroundColor DarkRed
        }
    } | Out-File $temp
}
end {
    Notepad $temp
}

 

<#

.SYNOPSIS
 本スクリプトはオンプレExchangeLegacyDN を再設定するスクリプトである。
 実行はExchangeサーバ上で行う。本スクリプトはExchange2013で実行する

.DESCRIPTION
本スクリプトと同一フォルダ内に「o365_module.psm1」が必要となる。

.PARAMETER userlist
 実行時に引数userlistを指定することで、読み込むユーザーリストを指定することができる。

.EXAMPLE
 # C:\exo\05-3.Set_LegacyExchangeDn.ps1 -userlist C:\exx\send\Getmailbox_before.csv

#>

param (
 [string]$userlist
)

$ErrorActionPreference = "STOP"

#日付指定
$execdate = Get-Date -Format yyyyMMddhhmmss

#関連ファイル指定
$currentdir = Split-Path $MyInvocation.Mycommand.Path -Parent
$Global:logdir = Join-Path $currentdir "Logs"
$module_file = Join-Path $currentdir "o365module.psm1"

#ログ出力
$Global:normal_logfile_path = Join-Path $logdir ($execdate + "_Set_LegacyExchangeDN.txt")
$Global:err_logfile_path = Join-Path $logdir ($execdate + "_Err_Set_LegacyExchangeDN.txt")

#再実行用リスト
$err_userlist = Join-Path $logdir ($execdate + "_Err_User_Set_LegacyExchangeDN.csv")
$err_userarray = New-Object System.Collections.ArrayList

$OK_count = 0
$AllCount = 0

$Global:msg = ""

if ((Test-Path($logdir)) -eq $false) {
 try {
  New-Item -ItemType Directory $logdir | Out-Null
 }
 catch {
  write-host (Get-Date) "ログフォルダを作成できませんでした。" -ForegroundColor Red
  write-host "スクリプト配置フォルダにおける ACL を見直してください。" -ForegroundColor Red
  write-host "スクリプトを終了するために何かキーを押してください。"
  $host.UI.RawUI.ReadKey() | Out-Null
  exit 1
 }
}

#デフォルトで指定されるユーザーリスト指定
if ([string]::IsNullOrEmpty($userlist)) {
 [string]$userlist = $usersetlist
}

if ((Test-Path($userlist)) -eq $false) {
 write-host (Get-Date) "バッチリストファイル(" + $userlist + ")が見つかりません。" -ForegroundColor Red
 $host.UI.RawUI.ReadKey() | Out-Null
 exit 1
}

#対象ユーザーリストのインポート
$targetuser = import-CSV $userlist -Encoding UTF8

#全体件数
$AllCount = (Get-Content -Path $Userlist).Length -1
write-host (Get-Date) "対象件数は $AllCount 件です。" -ForegroundColor White
$msg = "ユーザーリストを読み込みました。"
write-host (Get-Date) $msg -ForegroundColor White
$msg = ""

#共通モジュールの存在確認
if ((Test-Path($module_file)) -eq $false) {
 #エラー出力
 $msg = "共通モジュールが見つかりません。"
 write-host (Get-Date) $msg -ForcegroundColor Red
 $msg = ""
 $msg = "実行スクリプトと同一フォルダに共通モジュールファイルを配置してください。"
 write-host (Get-Date) $msg -ForcegroundColor Red
 $msg = ""
 $host.UI.RawUI.ReadKey() | Out-Nul
 exit 1
}
import-module (Join-Path $currentdir "o365module.psm1")

#エラーユーザー用配列に、ヘッダ情報を挿入
$err_userarray.Add("Name,LegacyExchangeDN") | Out-Null


#ExchangeLegacyDN 再設定

foreach($line in $targetuser){
 $username = $line.Name
 $LegacyExchangeDN = $line.LegacyExchangeDN
 
 try{

  Set-RemoteMailbox -Identity $username -EmailAddresses @{Add="X500:$LegacyExchangeDN"} -ErrorAction Stop

 }catch{  
  $msg = "ExchangeLegacyDN の再設定に失敗しました。対象ユーザー:" + $username
  write-host (Get-Date) $msg -ForegroundColor Yellow
  Logoutput -logfile $err_logfile_path -rc 9 -description $msg
  $msg = ""
  Logoutput -logfile $err_logfile_path -rc 8 -description $error[0]
  $err_userarray.Add("$username,$LegacyExchangeDN,$Full")
  continue
 }

 $OK_count++
}

$err_userarray | out-File $err_userlist -Encoding utf8

$err_count = $AllCount - $OK_count

$msg =  "対象件数は $AllCount 件です。"
write-host (Get-Date) $msg -ForegroundColor White
Logoutput -logfile $normal_logfile_path  -rc 0 -description $msg
$msg = ""

$msg =  "エラーとなった件数は $err_count 件です。"
write-host (Get-Date) $msg -ForegroundColor White
Logoutput -logfile $normal_logfile_path  -rc 0 -description $msg
$msg = ""

$msg =  "成功件数は $OK_count 件です。"
write-host (Get-Date) $msg -ForegroundColor White
Logoutput -logfile $normal_logfile_path  -rc 0 -description $msg
$msg = ""


$msg = "すべてのユーザーのExchangeLegacyDN 設定が完了しました。エラーとなった内容については、エラーファイルを参照してください。"
write-host (Get-Date) $msg -ForegroundColor White
Logoutput -logfile $normal_logfile_path -rc 0 -description $msg
$msg = ""