Advertisement
timsstuff

Extract-ExchangeStructure.ps1

Oct 23rd, 2015
1,212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Import-Module ActiveDirectory
  2. if(!(Get-PSSnapin |
  3.     Where-Object {$_.name -eq "Microsoft.Exchange.Management.PowerShell.E2010"})) {
  4.       ADD-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
  5. }
  6.  
  7. $start = Get-Date
  8. $fullPathIncFileName = $MyInvocation.MyCommand.Definition
  9. $currentScriptName = $MyInvocation.MyCommand.Name
  10. $currentpath = $fullPathIncFileName.Replace($currentScriptName, "")
  11.  
  12. $filepath = Read-Host "Enter path to save data (default [$currentpath]) "
  13. $domain = Read-Host "Enter domain suffix (default [contoso.com]) "
  14. if ($domain -eq "") {$domain = "contoso.com"}
  15. if ($filepath -eq "") {$filepath = $currentpath}
  16. if ($filepath.substring($filepath.length-1, 1) -ne "\") { $filepath = $filepath + "\" }
  17. $smlpath = $filepath.Trim()
  18. $xmlmailboxes = $filepath + "Mailboxes.xml"
  19. $xmldistributiongroups = $filepath + "DistributionGroups.xml"
  20. $nl = "`r`n"
  21. [int]$loop = 0
  22.  
  23. Write-Host $nl
  24. Write-Host "Starting Extraction at $start"
  25.  
  26. function DumpObjectToXml($obj, $addl) {
  27.     $a = $obj
  28.     if ($a -ne $null) {
  29.         $name = $a.GetType().Name
  30.         $openingTag = '<' + $a.GetType().Name + '>' + $nl
  31.         Set-Variable -Name ret -Value $openingTag -Scope 0
  32.  
  33.         Get-Member -InputObject $a -MemberType Properties | ForEach-Object {
  34.             Set-Variable -Name val -Scope 0
  35.             $CurrentName = $_.Name
  36.             $a.GetType().GetProperties() | ? { $_.Name -eq $CurrentName } | ForEach-Object {
  37.                 $specialSerialization = $false
  38.  
  39.                 # Handle specialized serialization for object properties of parent object
  40.                 Write-Debug "CurrentName: $CurrentName"
  41.                 if ($_.CanRead -eq $true -and $specialSerialization -eq $false) {
  42.                     $CurrentNode = $_.GetValue($a, $null)
  43.                     $count = $CurrentNode.Count
  44.                     Write-Debug "val: $CurrentNode"
  45.                     Write-Debug "count:  $count"
  46.                     if($count -gt 1 -and $loop -lt 3) {
  47.                         $val = $null
  48.                         $CurrentNodeName = $CurrentNode.GetType().Name
  49.                         if($CurrentNodeName.Contains("``1")) { $CurrentNodeName = $CurrentNodeName.Replace("``1","") }
  50.                         if ($CurrentNodeName -ne "" -and $CurrentNodeName -ne $null) {
  51.                             $CurrentNode | ForEach-Object {
  52.                                 $val += '<' + $CurrentNodeName + '>' + $nl + (DumpObjectToXml -obj $_) + $nl + '</' + $CurrentNodeName + '>' + $nl
  53.                             }
  54.                         }
  55.                     }
  56.                     else {
  57.                         $val = $CurrentNode
  58.                         If($val -eq $null) { $val = "" }
  59.                         else { $val = [string]$val }
  60.                         If($val.Contains('&')) { $val = [string]$val.Replace('&', '&amp;') }
  61.                     }
  62.                 }
  63.             }
  64.        
  65.             $out = '<' + $_.Name + '>' + $val + '</' + $_.Name + '>' + $nl
  66.             $ret = $ret + $out
  67.         }
  68.        
  69.         $ret = $ret + $addl
  70.         $closingTag = '</' + $a.GetType().Name + '>' + $nl
  71.         $ret = $ret + $closingTag
  72.         $loop += 1
  73.     }
  74.     return $ret
  75. }
  76.  
  77. $f1 = "(mail=*@" + $domain + ")"
  78. $f2 = "(emailaddresses -like `"*@" + $domain + "`")"
  79.  
  80. #Get-Mailbox -ResultSize 2000 -filter $f2 | Export-CSV $xmlmailboxes
  81. Write-Host "Writing Exchange Mailboxes to $xmlmailboxes" -NoNewline
  82. $m = "<Mailboxes>" + $nl
  83. $mblist = Get-Mailbox -ResultSize 2000 -filter $f2
  84. $mblist | ForEach-Object {
  85.     $loop = 0
  86.     Write-Host "." -NoNewline
  87.     $sa = "<SendAs>" + $nl
  88.     $samembers = Get-RecipientPermission -Identity $_
  89.     $samembers | ForEach-Object { $sa += (DumpObjectToXml -obj $_) }
  90.     $sa += "</SendAs>" + $nl
  91.    
  92.     $sa += "<FullAccess>" + $nl
  93.     $famembers = Get-MailboxPermission -Identity $_
  94.     $famembers | ForEach-Object { $sa += (DumpObjectToXml -obj $_) }
  95.     $sa += "</FullAccess>" + $nl
  96.    
  97.     $m = $m + (DumpObjectToXml -obj $_ -addl $sa)
  98. }
  99. $m = $m + "</Mailboxes>" + $nl
  100. $m > $xmlmailboxes
  101. $m = $null
  102.  
  103.  
  104. Write-Host "Writing Exchange Distribution Groups to $xmldistributiongroups" -NoNewline
  105. $d = "<DistributionGroups>" + $nl
  106. $dglist = Get-DistributionGroup -ResultSize 2000 -filter $f2
  107. $dglist | ForEach-Object {
  108.     $loop = 0
  109.     Write-Host "." -NoNewline
  110.     $dm = "<Members>" + $nl
  111.     $members = Get-DistributionGroupMember -Identity $_
  112.     $members | ForEach-Object { $dm = $dm + (DumpObjectToXml -obj $_) }
  113.     $dm = $dm + "</Members>" + $nl
  114.     $loop = 0
  115.     $d = $d + (DumpObjectToXml -obj $_ -addl $dm)
  116. }
  117. $d = $d + "</DistributionGroups>" + $nl
  118. $d > $xmldistributiongroups
  119. $d = $null
  120.  
  121. Write-Host $nl
  122. $end = Get-Date
  123. $elapsed = $end - $start
  124. Write-Host "Finished extraction at $end ($elapsed)"
  125. echo "Done!"
  126. [gc]::collect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement