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

Badges
MCSE
Community

Cozumpark Bilisim Portali
Posted in Windows Powershell | No Comment | 2,448 views | 21/03/2015 12:28

These are IIS Website Binding Information properties that you can get using CIM via PowerShell.

1
2
3
4
5
6
7
8
# Get WebSites
$WebSites = Get-CimInstance -Namespace "root\MicrosoftIISv2" -ClassName "IIsWebServerSetting" -OperationTimeoutSec 15 -EA Stop
 
$WebSite = $WebSites[0];
 
# WebSite Default Document Information
[string]$WebSiteEnableDefaultDoc = $WebSite.EnableDefaultDoc
[array]$WebSiteDefaultDocs = $WebSite.DefaultDoc.Split(",")

You can find more properties in my blog.


Posted in Windows Powershell | 4 Comments | 2,874 views | 28/02/2015 09:31

Well, if you read first part, now we will continue with text manipulations on PowerShell.

Test File: 424390 lines, 200 MB Microsoft IIS Log

In first part, winner was “System.IO.StreamReader” so I’ll continue with that.

1. Let’s try a Replace on our script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$LogFilePath = "C:\large.log"
$FileStream = New-Object -TypeName IO.FileStream -ArgumentList ($LogFilePath), ([System.IO.FileMode]::Open), ([System.IO.FileAccess]::Read), ([System.IO.FileShare]::ReadWrite);
$ReadLogFile = New-Object -TypeName System.IO.StreamReader -ArgumentList ($FileStream, [System.Text.Encoding]::ASCII, $true);
 
[int]$LineNumber = 0;
 
# Read Lines
while (!$ReadLogFile.EndOfStream)
{
	$LogContent = $ReadLogFile.ReadLine()
	$LineNumber++
 
	$LogContent = $LogContent.Replace('\','\\')
}
 
$ReadLogFile.Close()

After replace, script execution time: 3.2394121 seconds.

So what happens if I use Regex instead of Replace?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$LogFilePath = "C:\large.log"
$FileStream = New-Object -TypeName IO.FileStream -ArgumentList ($LogFilePath), ([System.IO.FileMode]::Open), ([System.IO.FileAccess]::Read), ([System.IO.FileShare]::ReadWrite);
$ReadLogFile = New-Object -TypeName System.IO.StreamReader -ArgumentList ($FileStream, [System.Text.Encoding]::ASCII, $true);
 
[int]$LineNumber = 0;
 
# Read Lines
while (!$ReadLogFile.EndOfStream)
{
	$LogContent = $ReadLogFile.ReadLine()
	$LineNumber++
 
	$LogContent = $LogContent -replace "\\", "\\\\"
}
 
$ReadLogFile.Close()

Now script execution time: 25.1311866 seconds. So .Net Replace is your best friend :)

Winner: Replace

2. What happens if I use -notlike in my text operation?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$LogFilePath = "C:\large.log"
$FileStream = New-Object -TypeName IO.FileStream -ArgumentList ($LogFilePath), ([System.IO.FileMode]::Open), ([System.IO.FileAccess]::Read), ([System.IO.FileShare]::ReadWrite);
$ReadLogFile = New-Object -TypeName System.IO.StreamReader -ArgumentList ($FileStream, [System.Text.Encoding]::ASCII, $true);
 
[int]$LineNumber = 0;
[int]$TestCount = 0;
 
# Read Lines
while (!$ReadLogFile.EndOfStream)
{
	$LogContent = $ReadLogFile.ReadLine()
	$LineNumber++
 
	$LogContent = $LogContent.Replace('\','\\')
 
	if ($LogContent -notlike "#*")
	{
		$TestCount++
	}
}
 
$ReadLogFile.Close()

Script takes 50.1493736 seconds.

But do I have another way for this query? Yes, I can use something like this. Let’s try -ne:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$LogFilePath = "C:\large.log"
$FileStream = New-Object -TypeName IO.FileStream -ArgumentList ($LogFilePath), ([System.IO.FileMode]::Open), ([System.IO.FileAccess]::Read), ([System.IO.FileShare]::ReadWrite);
$ReadLogFile = New-Object -TypeName System.IO.StreamReader -ArgumentList ($FileStream, [System.Text.Encoding]::ASCII, $true);
 
[int]$LineNumber = 0;
[int]$TestCount = 0;
 
# Read Lines
while (!$ReadLogFile.EndOfStream)
{
	$LogContent = $ReadLogFile.ReadLine()
	$LineNumber++
 
	$LogContent = $LogContent.Replace('\','\\')
 
	if ($LogContent.Substring(0,1) -ne "#")
	{
		$TestCount++
	}
}
 
$ReadLogFile.Close()

Script takes 25.3682308 seconds. OMG! :)

So using -eq/-ne queries 50% faster than -like/-notlike queries. Try to use them if it’s possible.

Winner: -EQ

To be continued.. :)


Posted in Windows Powershell | 4 Comments | 8,514 views | 28/02/2015 04:08

I want to give some performance tips for large text operations on PowerShell.

Test File: 424390 lines, 200 MB Microsoft IIS Log

1. First of all, we have to read file :) Lets try our alternatives:

a. Native command: Get-Content

1
2
3
4
5
6
7
8
9
$LogFilePath = "C:\large.log"
$Lines = Get-Content $LogFilePath
[int]$LineNumber = 0;
 
# Read Lines
foreach ($Line in $Lines)
{
	$LineNumber++
}

If I use this option, script takes: 13.3727013 seconds to read and loop in 424390 lines.

But how about memory usage?

getcontentmemory

Get-Content stores file into memory, so it’s normal to see high memory usage.

b. Using .Net method: [io.file]::ReadAllLines

1
2
3
4
5
6
7
8
9
$LogFilePath = "C:\large.log"
$Lines = [io.file]::ReadAllLines($LogFilePath)
[int]$LineNumber = 0;
 
# Read Lines
foreach ($Line in $Lines)
{
	$LineNumber++
}

In this option, script takes: 2.0082615 seconds to read and loop in 424390 lines which is extremely fast instead of Get-Content.

Memory usage is less than Get-Content but still too much. Also I can’t capture it but CPU is max 13%.

iofilememory

c. Using .Net method: System.IO.StreamReader

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$LogFilePath = "C:\large.log"
$FileStream = New-Object -TypeName IO.FileStream -ArgumentList ($LogFilePath), ([System.IO.FileMode]::Open), ([System.IO.FileAccess]::Read), ([System.IO.FileShare]::ReadWrite);
$ReadLogFile = New-Object -TypeName System.IO.StreamReader -ArgumentList ($FileStream, [System.Text.Encoding]::ASCII, $true);
 
[int]$LineNumber = 0;
 
# Read Lines
while (!$ReadLogFile.EndOfStream)
{
	$LogContent = $ReadLogFile.ReadLine()
	$LineNumber++
}
 
$ReadLogFile.Close()

If I use this option, script takes: 1.7062244 seconds to read and loop in 424390 lines. This seems fastest method.

Also memory usage is too low because it reads file line by line. So PowerShell doesn’t hold file in memory.

streammemory

But in this case, CPU usage is still too high. Probably it’s killing server’s one core at running time. But it’s something that I can’t help :)

Winner: System.IO.StreamReader

In next part, I’ll show you text manipulation tips. See you.


Posted in Hosting & IIS7, Windows Powershell | 1 Comment | 4,995 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 | 2,636 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 | 7,526 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! :)