Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Import-Module ActiveDirectory
- if(!(Get-PSSnapin |
- Where-Object {$_.name -eq "Microsoft.Exchange.Management.PowerShell.E2010"})) {
- ADD-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
- }
- $start = Get-Date
- $fullPathIncFileName = $MyInvocation.MyCommand.Definition
- $currentScriptName = $MyInvocation.MyCommand.Name
- $currentpath = $fullPathIncFileName.Replace($currentScriptName, "")
- $filepath = Read-Host "Enter path to save data (default [$currentpath]) "
- $domain = Read-Host "Enter domain suffix (default [contoso.com]) "
- if ($domain -eq "") {$domain = "contoso.com"}
- if ($filepath -eq "") {$filepath = $currentpath}
- if ($filepath.substring($filepath.length-1, 1) -ne "\") { $filepath = $filepath + "\" }
- $smlpath = $filepath.Trim()
- $xmlmailboxes = $filepath + "Mailboxes.xml"
- $xmldistributiongroups = $filepath + "DistributionGroups.xml"
- $nl = "`r`n"
- [int]$loop = 0
- Write-Host $nl
- Write-Host "Starting Extraction at $start"
- function DumpObjectToXml($obj, $addl) {
- $a = $obj
- if ($a -ne $null) {
- $name = $a.GetType().Name
- $openingTag = '<' + $a.GetType().Name + '>' + $nl
- Set-Variable -Name ret -Value $openingTag -Scope 0
- Get-Member -InputObject $a -MemberType Properties | ForEach-Object {
- Set-Variable -Name val -Scope 0
- $CurrentName = $_.Name
- $a.GetType().GetProperties() | ? { $_.Name -eq $CurrentName } | ForEach-Object {
- $specialSerialization = $false
- # Handle specialized serialization for object properties of parent object
- Write-Debug "CurrentName: $CurrentName"
- if ($_.CanRead -eq $true -and $specialSerialization -eq $false) {
- $CurrentNode = $_.GetValue($a, $null)
- $count = $CurrentNode.Count
- Write-Debug "val: $CurrentNode"
- Write-Debug "count: $count"
- if($count -gt 1 -and $loop -lt 3) {
- $val = $null
- $CurrentNodeName = $CurrentNode.GetType().Name
- if($CurrentNodeName.Contains("``1")) { $CurrentNodeName = $CurrentNodeName.Replace("``1","") }
- if ($CurrentNodeName -ne "" -and $CurrentNodeName -ne $null) {
- $CurrentNode | ForEach-Object {
- $val += '<' + $CurrentNodeName + '>' + $nl + (DumpObjectToXml -obj $_) + $nl + '</' + $CurrentNodeName + '>' + $nl
- }
- }
- }
- else {
- $val = $CurrentNode
- If($val -eq $null) { $val = "" }
- else { $val = [string]$val }
- If($val.Contains('&')) { $val = [string]$val.Replace('&', '&') }
- }
- }
- }
- $out = '<' + $_.Name + '>' + $val + '</' + $_.Name + '>' + $nl
- $ret = $ret + $out
- }
- $ret = $ret + $addl
- $closingTag = '</' + $a.GetType().Name + '>' + $nl
- $ret = $ret + $closingTag
- $loop += 1
- }
- return $ret
- }
- $f1 = "(mail=*@" + $domain + ")"
- $f2 = "(emailaddresses -like `"*@" + $domain + "`")"
- #Get-Mailbox -ResultSize 2000 -filter $f2 | Export-CSV $xmlmailboxes
- Write-Host "Writing Exchange Mailboxes to $xmlmailboxes" -NoNewline
- $m = "<Mailboxes>" + $nl
- $mblist = Get-Mailbox -ResultSize 2000 -filter $f2
- $mblist | ForEach-Object {
- $loop = 0
- Write-Host "." -NoNewline
- $sa = "<SendAs>" + $nl
- $samembers = Get-RecipientPermission -Identity $_
- $samembers | ForEach-Object { $sa += (DumpObjectToXml -obj $_) }
- $sa += "</SendAs>" + $nl
- $sa += "<FullAccess>" + $nl
- $famembers = Get-MailboxPermission -Identity $_
- $famembers | ForEach-Object { $sa += (DumpObjectToXml -obj $_) }
- $sa += "</FullAccess>" + $nl
- $m = $m + (DumpObjectToXml -obj $_ -addl $sa)
- }
- $m = $m + "</Mailboxes>" + $nl
- $m > $xmlmailboxes
- $m = $null
- Write-Host "Writing Exchange Distribution Groups to $xmldistributiongroups" -NoNewline
- $d = "<DistributionGroups>" + $nl
- $dglist = Get-DistributionGroup -ResultSize 2000 -filter $f2
- $dglist | ForEach-Object {
- $loop = 0
- Write-Host "." -NoNewline
- $dm = "<Members>" + $nl
- $members = Get-DistributionGroupMember -Identity $_
- $members | ForEach-Object { $dm = $dm + (DumpObjectToXml -obj $_) }
- $dm = $dm + "</Members>" + $nl
- $loop = 0
- $d = $d + (DumpObjectToXml -obj $_ -addl $dm)
- }
- $d = $d + "</DistributionGroups>" + $nl
- $d > $xmldistributiongroups
- $d = $null
- Write-Host $nl
- $end = Get-Date
- $elapsed = $end - $start
- Write-Host "Finished extraction at $end ($elapsed)"
- echo "Done!"
- [gc]::collect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement