Advertisement
timsstuff

Import-ExchangeStructure.ps1

Oct 23rd, 2015
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [CmdletBinding()]
  2. param(
  3.     [ValidateScript({Test-Path $_ -PathType Leaf})]
  4.     [Parameter(Mandatory=$True,Position=1)][string]$XMLFile
  5. )
  6.  
  7. Import-Module ActiveDirectory
  8. if(!(Get-PSSnapin |
  9.     Where-Object {$_.name -eq "Microsoft.Exchange.Management.PowerShell.E2010"})) {
  10.       ADD-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
  11. }
  12.  
  13. $logfile = ".\ImportExchangeStructure.log"
  14. $domain = Get-ADDomain
  15. $addomain = $domain.Forest
  16. $domaindn = $domain.DistinguishedName
  17. $ou = "$addomain/Exchange/Distribution Lists"
  18.  
  19. function writelog([string]$message) {
  20.     $timestamp = Get-Date
  21.     $fc = "White"
  22.     if($message.contains("ERROR")) {$fc ="Red"}
  23.     Add-content $logfile -value "$timestamp : $message"
  24.     Write-Host $message -ForegroundColor $fc
  25. }
  26.  
  27. function MBinDG([string]$lookinDG, [string]$findname) {
  28.     Set-Variable -Name userexists -Value $false -Scope 0
  29.     $userMB = Get-Mailbox $findname -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
  30.     $lookinDGmbrs = Get-DistributionGroupMember -Identity $lookinDG -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
  31.     $lookinDGmbrs | ForEach-Object {
  32.         if($_.Name -eq $userMB.Name) { $userexists = $true }
  33.     }
  34.     return $userexists
  35. }
  36.  
  37. $addomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().Name
  38. $me = [Environment]::UserName
  39. $nl = "`r`n"
  40.  
  41. [xml]$dglist = Get-Content $XMLFile
  42. ForEach($dg in $dglist.DistributionGroups.DistributionGroup) {
  43.     $DisplayName = $dg.DisplayName.Replace("OLDDOMAIN","NEWDOMAIN")
  44.     $Name = $dg.Name.Replace("OLDDOMAIN","NEWDOMAIN")
  45.     $Alias = $dg.Alias.Replace("OLDDOMAIN","NEWDOMAIN")
  46.     $ManagedBy = $dg.ManagedBy
  47.     $ModeratedBy = $dg.ModeratedBy
  48.     $Members = $dg.Members
  49.     $testdg = Get-DistributionGroup $Alias -ErrorAction SilentlyContinue
  50.    
  51.     Write-Host "Name: $Name"
  52.     Write-Host "Alias: $Alias"
  53.    
  54.     [String[]]$MemberList = ForEach($m in $Members.ReducedRecipient) {
  55.         Set-Variable -Name testmb -Value $null -Scope 0
  56.         Set-Variable -Name testdgm -Value $null -Scope 0
  57.         $mbr = $m.PrimarySmtpAddress
  58.         if($mbr -ne "" -and $mbr -ne $null) {
  59.             $mbr = $mbr.Replace("OLDDOMAIN","NEWDOMAIN")
  60.             if($mbr.Contains("contoso") -or $mbr.Contains("microsoft")) {
  61.                 $testmb = Get-Mailbox $mbr -ResultSize 1 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
  62.                 $testdgm = Get-DistributionGroup $mbr -ResultSize 1 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
  63.                 if($testmb -ne $null -or $testdgm -ne $null) {
  64.                     $mbr
  65.                 }
  66.                 else {
  67.                     writelog "ERROR: Could find mailbox or distribution group $mbr"
  68.                 }
  69.             }
  70.         }
  71.     }
  72.     [String[]]$ManagedByList = ForEach($m in $ManagedBy.ADMultiValuedProperty.ADObjectId) {
  73.         Set-Variable -Name testmb -Value $null -Scope 0
  74.         $mbr = $m.PrimarySmtpAddress
  75.         if($mbr -ne "" -and $mbr -ne $null) {
  76.             $mbr = $mbr.Replace("OLDDOMAIN","NEWDOMAIN")
  77.             $testmb = Get-Mailbox $mbr -ResultSize 1 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
  78.             if($testmb -ne $null) {
  79.                 $mbr
  80.             }
  81.         }
  82.     }
  83.     [String[]]$ModeratedByList = ForEach($m in $ModeratedBy.ADMultiValuedProperty.ADObjectId) {
  84.         Set-Variable -Name testmb -Value $null -Scope 0
  85.         $mbr = $m.PrimarySmtpAddress
  86.         if($mbr -ne "" -and $mbr -ne $null) {
  87.             $mbr = $mbr.Replace("OLDDOMAIN","NEWDOMAIN")
  88.             $testmb = Get-Mailbox $mbr -ResultSize 1 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
  89.             if($testmb -ne $null) {
  90.                 $mbr
  91.             }
  92.         }
  93.     }
  94.  
  95.     $mc = $MemberList.Count
  96.     if($ManagedByList -eq $null) {
  97.         [String[]]$ManagedByList = @($me)
  98.     }
  99.     Write-Host "Members ($mc members): $MemberList"
  100.     Write-Host "Managed By: $ManagedByList"
  101.     Write-Host "Moderated By: $ModeratedByList"
  102.    
  103.     If($testdg -eq $null) {
  104.         writelog "Creating Distribution Group $DisplayName"
  105.         New-DistributionGroup $Name -Alias $Alias -DisplayName $DisplayName -ManagedBy $ManagedByList `
  106.         -ModeratedBy $ModeratedByList -Members $MemberList -Type Distribution -OrganizationalUnit $ou
  107.     }
  108.     else {
  109.         ForEach($mdn in $MemberList) { if($mdn -ne $null) {
  110.             if($mdn.Trim() -ne "") {
  111.                 $useralreadyexists = MBinDG $Alias $mdn
  112.                 if($useralreadyexists -eq $false) {
  113.                     writelog "Adding $mdn to $DisplayName"
  114.                     Add-DistributionGroupMember $Name -Member $mdn -BypassSecurityGroupManagerCheck -ErrorAction SilentlyContinue
  115.                 }
  116.             }
  117.         } }
  118.     }    
  119.     Write-Host $nl
  120. }
  121. $dglist = $null
  122. [gc]::collect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement