search
Categories
Archive
Blogroll

Badges

Microsoft PowerShell MVP
MCSE
Sponsors

VirtualMetric
Cozumpark Bilisim Portali
Posted in Hosting & IIS7, Windows Powershell | No Comment | 196 views | 16/01/2015 14:01

You can get active IIS log file paths with following script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Get Web Sites
$WebSites = Get-Website | Select Name, Id, LogFile
 
foreach ($WebSite in $WebSites)
{
	# Clear Variables
	$LogFiles = $Null;
	$LogFilePath = $Null;
 
	# Get Web Site Information
	$SiteName = $WebSite.Name
	$SiteID = $WebSite.Id
 
	# Get Web Site Log Path
	$LogDirectory = $WebSite.LogFile.Directory -Replace '%SystemDrive%', $env:SystemDrive
	$LogPath = $LogDirectory + "\W3SVC" +  $SiteID
	$LogFiles = Get-ChildItem $LogPath -Filter *.log -EA SilentlyContinue | Sort-Object LastWriteTime -Descending
	if ($LogFiles) { $LogFilePath = $LogFiles[0].FullName; $LogFilePath; }
}

That will give you path as an output.


Posted in Windows Powershell | No Comment | 129 views | 31/12/2014 16:49

Merhaba,

CozumParkTV’ye 4 bölümlük PowerShell ile Hyper-V Yönetimi başlıklı eğitim videosu ekledim.

Eğitimde kullandığım komutları aşağıda görebilirsiniz:

1
2
3
4
5
6
7
Get-VM | Where {$_.NetworkAdapters.VlanSetting.AccessVlanId -eq "173"}
Get-VM | Get-VMHardDiskDrive | Where Path -like Disk*
Get-VM | Where ConfigurationLocation -like "C:\VM\*"
Get-VM | Measure-Object -Property ProcessorCount –Sum
Get-VM | Select Name, SizeOfSystemFiles | Sort SizeOfSystemFiles
Move-VM VMName -DestinationHost VMHost -IncludeStorage -DestinationStoragePath C:\ClusterStorage\Volume1\VMName
Move-VMStorage -VMName VMName -DestinationStoragePath C:\ClusterStorage\Volume1\VMName"

Ayrıca senaryo adımlarını da aşağıda paylaşıyorum:

1
2
3
4
5
6
7
8
9
10
11
12
13
TestVM isimli bir VM yaratıp, konfigürasyon dosyalarının C:\VMs altında TestVM isimli bir klasör altında olmasını sağlayın.
TestSwitch isimli Private VM Switch yaratın ve VM’in ilk network adaptörünün bu switch’e bağlı olmasını sağlayın.
VM’in VLAN ID olarak 132 kullanmasını sağlayın.
VM’in CPU ve Memory değerlerini, 4 işlemci ve 2 GB olarak ayarlayın.
Yoksa DVD sürücü ekleyin ve kurulum ISO’sunu VM’e bağlayın.
VM’in bu hali için snapshot alın.
VM’i çalıştırın
VM açık durumda iken C:\MovedVMs altında TestVM isimli klasörün içinde olacak şekilde VM’i Live Storage Migration yapın.
VM’i durdurun
VM’in memory’sini dynamic memory olarak güncelleyin. Açılış 1 Gb, minimum 512 Mb, maksimum da 2 Gb şekilde ayarlayın.
VM’i tekrar açın
Çalışmakta olan VM’i, son snapshot noktasına geri döndürün.
VM’de bulunan iso dosyasını dismount edin.

Eğitimle ilgili sorularınız varsa bana yukardaki iletişim bölümü üzerinden ulaşabilirsiniz.


Posted in Exchange Server, Windows Powershell | No Comment | 389 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 | 355 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 | 382 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 | 229 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 | 430 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.