search
Categories
Archive
Blogroll

Badges

Microsoft PowerShell MVP
MCSE
Sponsors

VirtualMetric
Cozumpark Bilisim Portali
Posted in Exchange Server, Windows Powershell | No Comment | 194 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 | 214 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 | 234 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 | 153 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 | 278 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 Windows Powershell | No Comment | 250 views | 15/11/2014 10:37

This is a text mining example for PowerShell. You can export your Whatsapp conversations
to find out which words you used most.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$Contents = Get-Content Whatsapp.txt -Encoding UTF8
$TagCloud = @{"Whatsapp" = "1"}
$ChatArray = New-Object System.Collections.ArrayList
foreach ($Content in $Contents)
{
	$Words = $Content.Split(" ")
	foreach ($Word in $Words)
	{
		$Word = $Word.ToLower();
 
		if ($ChatArray.Contains($Word) -eq $True)
		{
			$TagCloud.($Word) = [int]$TagCloud.($Word)+1;
		}
		else
		{
			$AddArray = $ChatArray.Add("$Word")
			$TagCloud.($Word) = 1;
		}
	}
}
$TagCloud.GetEnumerator() | Sort-Object -Property Value -Descending

Then you can convert it to tag cloud by using public tag cloud services.


Posted in Windows Powershell | No Comment | 273 views | 31/10/2014 17:39

If you create a secure password on PowerShell and output it into a txt file, you can decrypt it on another Windows machine. So we use a predefined user key to create our secure passwords:

There was a question about this post. So how you can create your own key?

So what you need to do is:

1
2
3
4
$SecureKey = New-Object byte[](16)
$RNGCryptoServiceProvider = [System.Security.Cryptography.RNGCryptoServiceProvider]::Create()
$RNGCryptoServiceProvider.GetBytes($SecureKey)
$SecureKey

That will create a random key for you. So you can replace it with predefined key.