search
Categories
Sponsors
VirtualMetric Hyper-V Monitoring, Hyper-V Reporting
Archive
Blogroll

Badges
MCSE
Community

Cozumpark Bilisim Portali
Exchange Server 2010/2013 Config Backup and Restore (v1.1)
Posted in Exchange Server, Windows Powershell | 2 Comments | 2,174 views | 27/11/2014 02:00

You may have many reasons to get your config backup of Exchange Server instead of Windows backup. For example, you may want to create a test environment. So in that case, you can use this script to backup your Exchange Server 2010 environment and restore to new Exchange Server 2013.

This script is in v1.1. Backups and restores Accepted Domains, Mailboxes, User Information, Mailbox Permissions, Mail Contacts, Distribution Groups.

First backup your Exchange Server:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Backup Accepted Domains
$AcceptedDomains = Get-AcceptedDomain | ConvertTo-Json
$AcceptedDomains | Set-Content -Path AcceptedDomains.txt
 
# Backup Mailboxes
$Mailboxes = Get-Mailbox -RecipientTypeDetails UserMailbox | ConvertTo-Json
$Mailboxes | Set-Content -Path Mailboxes.txt
 
# Backup User Information
$Users = Get-User | ConvertTo-Json
$Users | Set-Content -Path Users.txt
 
# Backup Mailbox Permissions
$MailboxPermissions = Get-Mailbox -RecipientTypeDetails UserMailbox | Get-MailboxPermission | where {$_.user.tostring() -ne "NT AUTHORITY\SELF" -and $_.IsInherited -eq $false} | Select @{Name='Name';Expression={[string]$_.Identity.Name}},@{Name='AccessRights';Expression={[string]::join(', ', $_.AccessRights)}},@{Name='User';Expression={[string]$_.User.RawIdentity}} | ConvertTo-Json
$MailboxPermissions | Set-Content -Path MailboxPermissions.txt
 
# Backup Mail Contacts
$MailContacts = Get-MailContact | ConvertTo-Json
$MailContacts | Set-Content -Path MailContacts.txt
 
# Backup Distribution Groups
$DistributionGroups = Get-DistributionGroup | Select *, @{label="Members";expression={($_ | Get-DistributionGroupMember).Name}} | ConvertTo-Json
$DistributionGroups | Set-Content -Path DistributionGroups.txt

You will see config backups under your current PowerShell path. Transfer them to new Exchange server.
You can restore all data with following script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Load JSon Serialization
$LoadJson = [System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
$JsonSerial= New-Object -TypeName System.Web.Script.Serialization.JavaScriptSerializer
$JsonSerial.MaxJsonLength = [int]::MaxValue
 
# Restore Accepted Domains
$AcceptedDomainsRaw = Get-Content -Path AcceptedDomains.txt -Raw
$AcceptedDomains = $JsonSerial.DeserializeObject($AcceptedDomainsRaw)
 
# Restore Mailboxes
$MailboxesRaw = Get-Content -Path Mailboxes.txt -Raw
$Mailboxes = $JsonSerial.DeserializeObject($MailboxesRaw)
 
# Restore User Information
$UsersRaw = Get-Content -Path Users.txt -Raw
$Users = $JsonSerial.DeserializeObject($UsersRaw)
 
# Restore Mailbox Permissions
$MailboxPermissionsRaw = Get-Content -Path MailboxPermissions.txt -Raw
$MailboxPermissions = $JsonSerial.DeserializeObject($MailboxPermissionsRaw)
 
# Restore Mail Contacts
$MailContactsRaw = Get-Content -Path MailContacts.txt -Raw
$MailContacts = $JsonSerial.DeserializeObject($MailContactsRaw)
 
# Restore Distribution Groups
$DistributionGroupsRaw = Get-Content -Path DistributionGroups.txt -Raw
$DistributionGroups = $JsonSerial.DeserializeObject($DistributionGroupsRaw)
 
Write-Host "Working on Accepted Domains.."
Write-Host " "
 
# Set Accepted Domains
foreach ($AcceptedDomain in $AcceptedDomains)
{
	$CheckAcceptedDomain = Get-AcceptedDomain | Where DomainName -eq $AcceptedDomain.DomainName.Address
	if (!$CheckAcceptedDomain)
	{
		Write-Host $AcceptedDomain.DomainName.Address is not exist. Creating.. -ForegroundColor Red
		$NewAcceptedDomain = New-AcceptedDomain -Name $AcceptedDomain.Name -DomainName $AcceptedDomain.DomainName.Address
	}
	else
	{
		Write-Host $AcceptedDomain.Name is already available. -ForegroundColor Green
	}
}
 
Write-Host " "
Write-Host " "
Write-Host "Working on Mailboxes.."
Write-Host " "
 
# Set Variables
$Database = "FABRIKAM"
$OrganizationalUnit = "fabrikam.com/Contoso"
$SecurePassword = ConvertTo-SecureString "12qwaszxcv!" -AsPlainText -Force
 
# Set Mailboxes
foreach ($Mailbox in $Mailboxes)
{
	if ($Mailbox.RecipientTypeDetails -eq "UserMailbox")
	{
		$CheckMailbox = Get-Mailbox | Where {$_.SamAccountName -eq $Mailbox.SamAccountName}
		if (!$CheckMailbox)
		{
			Write-Host $Mailbox.SamAccountName is not exist. Creating.. -ForegroundColor Red
 
			#$Database = $Mailbox.Database
			#$OrganizationalUnit = $Mailbox.OrganizationalUnit
			$NewMailbox = New-Mailbox -Password $SecurePassword -UserPrincipalName $Mailbox.UserPrincipalName -Name $Mailbox.Name -Database $Database -DisplayName $Mailbox.DisplayName -FirstName $Mailbox.FirstName -Initials $Mailbox.Initials -LastName $Mailbox.LastName -OrganizationalUnit $OrganizationalUnit -PrimarySmtpAddress ($Mailbox.PrimarySmtpAddress.Local + "@" + $Mailbox.PrimarySmtpAddress.Domain) -SamAccountName $Mailbox.SamAccountName
		}
		else
		{
			Write-Host $Mailbox.SamAccountName is already available. -ForegroundColor Green
		}
	}
	else
	{
		Write-Host Skipping $Mailbox.SamAccountName discovery mailbox.. -ForegroundColor Green
	}
}
 
Write-Host " "
Write-Host " "
Write-Host "Working on Email Addresses.."
Write-Host " "
 
# Set Email Addresses
foreach ($Mailbox in $Mailboxes)
{
	if ($Mailbox.RecipientTypeDetails -eq "UserMailbox")
	{
		$MailboxEmailAddresses = $Mailbox.EmailAddresses
 
		$TargetMailbox = Get-Mailbox | Where {$_.SamAccountName -eq $Mailbox.SamAccountName}
 
		foreach ($MailboxEmailAddress in $MailboxEmailAddresses)
		{
			$EmailAddress = $MailboxEmailAddress.Remove(0,5)
 
			$CheckEmailAddress = $TargetMailbox.EmailAddresses | where SMTPAddress -eq $EmailAddress
 
			if (!$CheckEmailAddress)
			{				
				Write-Host $EmailAddress is not exist. Adding.. -ForegroundColor Red
				$AddEmailAddress = $TargetMailbox | Set-Mailbox -EmailAddresses @{add="$EmailAddress"}
			}
			else
			{
				Write-Host $EmailAddress is already available. -ForegroundColor Green
			}
		}
	}
	else
	{
		Write-Host Skipping $Mailbox.SamAccountName discovery mailbox.. -ForegroundColor Green
	}
}
 
Write-Host " "
Write-Host " "
Write-Host "Working on Mailbox Permissions.."
Write-Host " "
 
# Set Mailbox Permissions
foreach ($MailboxPermission in $MailboxPermissions)
{
	$MailboxPermissionUsername = "FABRIKAM\" + ($MailboxPermission.User).Split("\")[-1]
	$CheckMailboxPermission = Get-Mailbox -Identity $MailboxPermission.Name | Get-MailboxPermission | where {$_.user.tostring() -eq $MailboxPermissionUsername -and $_.AccessRights -eq $MailboxPermission.AccessRights}
 
	if (!$CheckMailboxPermission)
	{
		Write-Host $MailboxPermissionUsername doesnt have permission on $MailboxPermission.Name mailbox. Adding.. -ForegroundColor Red
		$AddMailboxPermission = Add-MailboxPermission -Identity $MailboxPermission.Name -User $MailboxPermissionUsername -AccessRights $MailboxPermission.AccessRights -InheritanceType All
	}
	else
	{
		Write-Host $MailboxPermissionUsername has permission on $MailboxPermission.Name mailbox. Skipping.. -ForegroundColor Green
	}
}
 
Write-Host " "
Write-Host " "
Write-Host "Working on Mail Contacts.."
Write-Host " "
 
# Set Variables
$OrganizationalUnit = "fabrikam.com/Contoso"
 
# Set Mail Contacts
foreach ($MailContact in $MailContacts)
{
	$CheckMailContact = Get-MailContact | Where Name -eq $MailContact.Name
	if (!$CheckMailContact)
	{
		#$OrganizationalUnit = $Mailbox.OrganizationalUnit
 
		Write-Host $MailContact.Name is not exist. Creating.. -ForegroundColor Red
		$AddMailContact = New-MailContact -Name $MailContact.Name -DisplayName $MailContact.DisplayName -ExternalEmailAddress $MailContact.ExternalEmailAddress.SmtpAddress -OrganizationalUnit $OrganizationalUnit
	}
	else
	{
		Write-Host $MailContact.Name is already available. -ForegroundColor Green
	}
}
 
Write-Host " "
Write-Host " "
Write-Host "Working on Distribution Groups.."
Write-Host " "
 
# Set Variables
$OrganizationalUnit = "fabrikam.com/Contoso"
 
# Set Distribution Groups
foreach ($DistributionGroup in $DistributionGroups)
{
	$CheckDistributionGroup = Get-DistributionGroup | Where Name -eq $DistributionGroup.Name
	if (!$CheckDistributionGroup)
	{
		#$OrganizationalUnit = $Mailbox.OrganizationalUnit
 
		Write-Host $DistributionGroup.Name is not exist. Creating.. -ForegroundColor Red
		$AddDistributionGroup = New-DistributionGroup -Name $DistributionGroup.Name -Alias $DistributionGroup.Alias -DisplayName $DistributionGroup.DisplayName -Members $DistributionGroup.Members.value -Notes $DistributionGroup.Notes -OrganizationalUnit $OrganizationalUnit -PrimarySmtpAddress ($DistributionGroup.PrimarySmtpAddress.Local + "@" + $DistributionGroup.PrimarySmtpAddress.Domain) -SamAccountName $DistributionGroup.SamAccountName -Type "Distribution"
	}
	else
	{
		Write-Host $DistributionGroup.Name is already available. -ForegroundColor Green
	}
}

There are some variables that you may can change. So you can restore backups into different Exchange environment as well.


Comments (2)

NeWay Technologies – Weekly Newsletter #123 – November 27, 2014 | NeWay

November 29th, 2014
16:46:46

[…] Exchange Server 2010/2013 Config Backup and Restore (v1.1) – […]


NeWay Technologies – Weekly Newsletter #123 – November 28, 2014 | NeWay

November 29th, 2014
16:53:15

[…] Exchange Server 2010/2013 Config Backup and Restore (v1.1) – […]



Leave a Reply