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

Badges
MCSE
Community

Cozumpark Bilisim Portali
Posted in Exchange Server, Windows Powershell | No Comment | 7,519 views | 29/11/2014 15:48

You may need get distribution group list by their sender authentication status.

If sender authentication is disabled:

Get-DistributionGroup | where RequireSenderAuthenticationEnabled -eq $false

If sender authentication is enabled:

Get-DistributionGroup | where RequireSenderAuthenticationEnabled -eq $true

That will output distribution groups.


Posted in Exchange Server, Windows Powershell | No Comment | 2,242 views | 27/11/2014 02:32

These are just notes for me to remember later :)

First you need to assign Mailbox Import / Export role to your user.

New-ManagementRoleAssignment –Role “Mailbox Import Export” –User "Administrator"

Now you should open a new PowerShell window to apply changes.

You can export your mailbox with following command:

New-MailboxExportRequest -Mailbox "yusuf" -FilePath "\\localhost\Archive\yusufozturk.pst"

You can import your pst file with following command:

New-MailboxImportRequest -Mailbox yusuf -FilePath "\\localhost\Archive\yusufozturk.pst"

You should check import / export activity with following commands:

Get-MailboxExportRequest
Get-MailboxImportRequest

Also you can check import status with following command:

Get-MailboxImportRequest | Get-MailboxImportRequestStatistics

That’s it! :)


Posted in Exchange Server, Windows Powershell | 2 Comments | 2,489 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.


Posted in Exchange Server, Windows Powershell | No Comment | 1,639 views | 26/11/2014 11:10

You can export txt based config backups of your Exchange Server 2010 environment 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
# Backup Accepted Domains
$AcceptedDomains = Get-AcceptedDomain | ConvertTo-Json
$AcceptedDomains | Set-Content -Path AcceptedDomains.txt
 
# Backup Mailboxes
$Mailboxes = Get-Mailbox | 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 | Get-MailboxPermission | where {$_.user.tostring() -ne "NT AUTHORITY\SELF" -and $_.IsInherited -eq $false} | Select Identity,User,@{Name='Access Rights';Expression={[string]::join(', ', $_.AccessRights)}} | 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

I’ll also publish an import script to show you how to restore this config into your system again.


Posted in Exchange Server, Windows Powershell | No Comment | 1,831 views | 26/11/2014 11:01

I looked for a powershell command to get distribution groups and members in one command but it seems it’s not possible.

So I used an expression to merge them. You can check my following code:

1
Get-DistributionGroup | Select Name, @{label="Members";expression={($_ | Get-DistributionGroupMember).Name}}

Output will be like:

Name Members
—- ——-
Customer Services {Yusuf Ozturk, Bertan Demirci}

You can see all other fields by using:

1
Get-DistributionGroup | Select *, @{label="Members";expression={($_ | Get-DistributionGroupMember).Name}}

You can also add other field by using additional expressions.


Posted in Exchange Server, Windows Powershell | No Comment | 2,934 views | 24/05/2013 14:02

You can get your db statistics like this:

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
# PoSHStats Pipeline
function PoSHStats-Pipeline 
{
	# Create Timer
	Begin { $i=0; $Timer = [Diagnostics.StopWatch]::StartNew() } 
 
	# Process Items
	Process { $i++; $_ }
 
	# Informational Output
	End { Write-Debug "Processed $i items in $($Timer.Elapsed.TotalSeconds) seconds." } 
}
 
$ExchangeServer = "EXMB04"
 
# Get MailboxDatabase
if ($ExchangeServer.IsMailboxServer -eq $True)
{
	Get-MailboxDatabase -Status | where {$_.Server -eq "$ExchangeHost"} | PoSHStats-Pipeline | %{
 
		# Database Statistics
		$DBName = $_.Name
		$DBSize = $_.DatabaseSize.ToGB()
		$DBLastFullBackup = $_.LastFullBackup.ToString()
		$DBWhiteSpace = $_.AvailableNewMailboxSpace.ToMB()
 
		# Mailbox Statistics
		$MBStats = Get-MailboxStatistics -Database $DBName | %{$_.TotalItemSize.value.ToMb()} | Measure-Object -Average
		$MBCount = $MBStats.Count
		$MBAvg = ([math]::round(($MBStats.Average), 0))
 
		# Mailbox Database Disk Usage
		$DBDrive = $_.EdbFilePath.DriveName
		$DBDiskInfo = Get-WmiObject -ComputerName $ExchangeHost -Class Win32_LogicalDisk | Where {$_.DeviceID -eq $DBDrive}
		$DBDiskSize = ([math]::round(($DBDiskInfo.Size / 1GB), 0))
		$DBDiskFreeSpace = ([math]::round(($DBDiskInfo.FreeSpace / 1GB), 0))
 
		# Exchange Database Disk Usage
		[int]$ExchangeDatabaseSize = [int]$ExchangeDatabaseSize + [int]$DBSize
		[int]$ExchangeDatabaseWhiteSpace = [int]$ExchangeDatabaseWhiteSpace + [int]$DBWhiteSpace
 
		# Database White Space
		[int]$DatabaseWhiteSpace = ([math]::round(($DBWhiteSpace / 1KB), 0))
 
		# Exchange Server Disk Usage
		$TestDrive = $DiskDrives.Contains("$DBDrive")
 
		if ($TestDrive -ne $True)
		{
			$DiskDrives.Add("$DBDrive") | Out-Null
			[int]$ExchangeDatabaseDiskSize = [int]$ExchangeDatabaseDiskSize + [int]$DBDiskSize
			[int]$ExchangeDatabaseDiskFreeSpace = [int]$ExchangeDatabaseDiskFreeSpace + [int]$DBDiskFreeSpace
		}
 
		# Host Total Database Size
		[int]$HostTotalDatabaseSize = [int]$HostTotalDatabaseSize + [int]$DBSize
 
		# Host Total Database Count
		[int]$HostTotalDatabaseCount++ | Out-Null
 
		# Host Total Mailbox Count
		[int]$HostTotalMailboxCount = [int]$HostTotalMailboxCount + [int]$MBCount
 
		# Host Total Mailbox Average
		[int]$HostTotalMailboxAverage = [int]$HostTotalMailboxAverage + [int]$MBAvg
	}
}

This is the preview code of PoSHStats Exchange Server module.


Posted in Exchange Server, Windows Powershell | 2 Comments | 13,183 views | 22/05/2013 16:45

You can get ready and retry queue’s of Exchange Server with this 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
$ExchangeHost = "ExCas01"
$ExchangeServer = Get-ExchangeServer -Identity $ExchangeHost
 
# Integers
[int]$ExchangeActiveConnections = 0;
[int]$ExchangeRetryQueue = 0;
[int]$ExchangeMessageQueue = 0;
[int]$ExchangeActiveQueue = 0;
[int]$HostMessageQueue = 0;
[int]$HostRetryQueue = 0;
 
# Get Mail Queue
if ($ExchangeServer.IsHubTransportServer -eq $True)
{
	# Message Queue
	$MessageQueue = Get-Queue -Server $ExchangeHost | Where {$_.Status -eq "Ready" -and $_.MessageCount -gt "0"}
 
	# Sum Message Queue
	Foreach ($Queue in $MessageQueue)
	{
		[int]$HostMessageQueue = [int]$HostMessageQueue + [int]$Queue.MessageCount
	}
 
	# Retry Queue
	$RetryQueue = Get-Queue -Server $ExchangeHost | Where {$_.Status -eq "Retry" -and $_.MessageCount -gt "0"}
 
	# Sum Retry Queue
	Foreach ($Queue in $RetryQueue)
	{
		[int]$HostRetryQueue = [int]$HostRetryQueue + [int]$Queue.MessageCount
	}
 
	# Exchange Queue
	[int]$ExchangeRetryQueue = [int]$ExchangeRetryQueue + [int]$HostRetryQueue
	[int]$ExchangeMessageQueue = [int]$ExchangeMessageQueue + [int]$HostMessageQueue
	[int]$ExchangeActiveQueue = [int]$ExchangeActiveQueue + [int]$HostRetryQueue + [int]$HostMessageQueue
}

You can add multiple Exchange hosts to an array, it’ll just query Hub Transport servers. It only support Exchange Server 2010.