Categories
Sponsors
Archive
Blogroll
Badges
Community
|
Posted in Virtual Machine Manager, Windows Powershell | No Comment | 7,685 views | 22/10/2010 10:59
In one of the Technet question, John said:
“When the Hyper-V role is enabled, the physical machine becomes a virtual machine known as the parent partition. The virtual machines are called child partitions. Each partition is isolated from the other. Task Manager in any partition shows only the load on the cpus in that partitions. If the child partitions are heavily taxing the physical cpus, Task Manager in the parent partition shows little cpu activity (assuming you don’t have some heavy load in the parent, which you shouldn’t – best practice is to not have things running in the parent).”
So that means, looking task manager is not a way to know exact resource usage of Hyper-V. If you need to monitor CPU usages of child partitions (virtual machines), you should use Perfmon or.. yes, Powershell :) I wrote a little script to see CPU usages. In my environment, we use Windows Server 2008 with Powershell v1, so I have to write it in version 1. But If you have Powershell v2, you surely can use Get-Counter command.
Function HyperVCPU($InstanceName)
{
$CategoryName = "Hyper-V Hypervisor Virtual Processor"
$CounterName = "% Guest Run Time"
$PerfCounter = New-Object System.Diagnostics.PerformanceCounter($CategoryName, $CounterName, $InstanceName)
$Result = $PerfCounter.NextValue()
$Result = $PerfCounter.NextValue()
$Result
}
Function ShowPerfmon
{
$PerfMon = New-Object System.Diagnostics.PerformanceCounterCategory
$PerfMon.CategoryName = "Hyper-V Hypervisor Virtual Processor"
$Instances = $PerfMon.GetInstanceNames()
Foreach ($Instance in $Instances)
{
$CPU = HyperVCPU $Instance
$Results = New-Object Psobject
$Results | Add-Member Noteproperty Name $Instance
$Results | Add-Member Noteproperty CPU $CPU
$Results
}
}
ShowPerfmon |
Function HyperVCPU($InstanceName)
{
$CategoryName = "Hyper-V Hypervisor Virtual Processor"
$CounterName = "% Guest Run Time"
$PerfCounter = New-Object System.Diagnostics.PerformanceCounter($CategoryName, $CounterName, $InstanceName)
$Result = $PerfCounter.NextValue()
$Result = $PerfCounter.NextValue()
$Result
}
Function ShowPerfmon
{
$PerfMon = New-Object System.Diagnostics.PerformanceCounterCategory
$PerfMon.CategoryName = "Hyper-V Hypervisor Virtual Processor"
$Instances = $PerfMon.GetInstanceNames()
Foreach ($Instance in $Instances)
{
$CPU = HyperVCPU $Instance
$Results = New-Object Psobject
$Results | Add-Member Noteproperty Name $Instance
$Results | Add-Member Noteproperty CPU $CPU
$Results
}
}
ShowPerfmon
But be careful. You need to run Powershell as an Administrator to reach perfmon counters. If not, you will get “Category does not exist” error.
Posted in Windows Powershell | No Comment | 5,149 views | 27/06/2010 21:46
If you use Flashget, sometimes it downloads files with .html extension. You have to trim .html extensions from files but if you download so many files, you need to use script to save your time.
$files = dir
foreach ($file in $files)
{
$new=$file.name.replace(".html","")
ren $file $new
} |
$files = dir
foreach ($file in $files)
{
$new=$file.name.replace(".html","")
ren $file $new
}
Simply, I use basic dos command, ren to rename files. But you can use Rename-Item to make it 100% Powershell :)
Posted in Windows Powershell | 1 Comment | 4,689 views | 17/06/2010 12:32
One of my colleague asked me to make a script to write out server specifications of our new Dell servers using with their Service Tags. Recently, We rented more than 200 Dell servers and that is real mess to know which one has 4 gb ram and which one has SAS raid etc. The quickiest way to know this, using their Service Tags. Because you can see servers hardware from Dell web page with Service Tags. My script simply use service tags to get hardware and parse it for a more readable format.
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
| $ServiceTags = Get-Content C:\ServiceTags.txt
$TagsDir = "C:\ServiceTags"
Foreach ($ServiceTag in $ServiceTags)
{
$SystemInfoURL = "http://support.dell.com/support/topics/global.aspx/support/my_systems_info/en/details?c=us&l=en&s=gen&servicetag=" + $ServiceTag + "&~tab=2&~wsf=tabs"
$Webclient = New-Object System.Net.WebClient
$Webpage = $Webclient.DownloadString($SystemInfoURL)
$Webpage > "$TagsDir\$ServiceTag.txt"
$HardDrive = Select-String "$TagsDir\$ServiceTag.txt" -Pattern 'Hard Drive'
$HardDrive = "$HardDrive" -match '<td class="([^"]+)" valign="top">([^"]+)</td><td class="([^"]+)" valign="top"></td><td class="([^"]+)" valign="top">Hard Drive,(.*?),([^"]+)</td>'
$HardDriveModel = $Matches[6]
$HardDriveModel = $HardDriveModel.Replace(" ","")
$HardDriveModel = $HardDriveModel.Replace("</td></tr></table>","")
$HardDriveCount = $Matches[2]
$HardDriveSize = $Matches[5]
$Processor = Select-String "$TagsDir\$ServiceTag.txt" -Pattern 'Processor'
$Processor = "$Processor" -match '<td class="([^"]+)" valign="top">([^"]+)</td><td class="([^"]+)" valign="top"></td><td class="([^"]+)" valign="top">Processor,(.*?),([^"]+)</td>'
$ProcessorModel = $Matches[6]
$ProcessorModel = $ProcessorModel.Replace(" ","")
$ProcessorModel = $ProcessorModel.Replace("</td></tr></table>","")
$ProcessorCount = $Matches[2]
$ProcessorSize = $Matches[5]
$Memory = Select-String "$TagsDir\$ServiceTag.txt" -Pattern 'Memory'
$Memory = "$Memory" -match '<td class="([^"]+)" valign="top">([^"]+)</td><td class="([^"]+)" valign="top"></td><td class="([^"]+)" valign="top">Dual In-line Memory Module,(.*?),([^"]+)</td>'
$MemoryModel = $Matches[6]
$MemoryModel = $MemoryModel.Replace(" ","")
$MemoryModel = $MemoryModel.Replace("</td></tr></table>","")
$MemoryCount = $Matches[2]
$MemorySize = $Matches[5]
$Raid = Select-String "$TagsDir\$ServiceTag.txt" -Pattern 'Assembly'
$Raid = "$Raid" -match '<td class="([^"]+)" valign="top">([^"]+)</td><td class="([^"]+)" valign="top"></td><td class="([^"]+)" valign="top">Assembly,Cable,(.*?),([^"]+)</td>'
$RaidModel = $Matches[5]
$RaidModel = $RaidModel.Replace(" ","")
$RaidModel = $RaidModel.Replace("</td></tr></table>","")
$RaidCount = $Matches[2]
$RaidType = $Matches[6]
Write-Host Service Tag: $ServiceTag -ForegroundColor Red
Write-Host Hard Drive: -ForegroundColor Green
write-host Adet: $HardDriveCount
write-host Kapasite: $HardDriveSize
write-host Model: $HardDriveModel
Write-Host Processor: -ForegroundColor Green
write-host Adet: $ProcessorCount
write-host Kapasite: $ProcessorSize
write-host Model: $ProcessorModel
Write-Host Memory: -ForegroundColor Green
write-host Adet: $MemoryCount
write-host Kapasite: $MemorySize
write-host Model: $MemoryModel
Write-Host Raid: -ForegroundColor Green
write-host Adet: $RaidCount
write-host Tip: $RaidType
write-host Model: $RaidModel
write-host
} |
$ServiceTags = Get-Content C:\ServiceTags.txt
$TagsDir = "C:\ServiceTags"
Foreach ($ServiceTag in $ServiceTags)
{
$SystemInfoURL = "http://support.dell.com/support/topics/global.aspx/support/my_systems_info/en/details?c=us&l=en&s=gen&servicetag=" + $ServiceTag + "&~tab=2&~wsf=tabs"
$Webclient = New-Object System.Net.WebClient
$Webpage = $Webclient.DownloadString($SystemInfoURL)
$Webpage > "$TagsDir\$ServiceTag.txt"
$HardDrive = Select-String "$TagsDir\$ServiceTag.txt" -Pattern 'Hard Drive'
$HardDrive = "$HardDrive" -match '<td class="([^"]+)" valign="top">([^"]+)</td><td class="([^"]+)" valign="top"></td><td class="([^"]+)" valign="top">Hard Drive,(.*?),([^"]+)</td>'
$HardDriveModel = $Matches[6]
$HardDriveModel = $HardDriveModel.Replace(" ","")
$HardDriveModel = $HardDriveModel.Replace("</td></tr></table>","")
$HardDriveCount = $Matches[2]
$HardDriveSize = $Matches[5]
$Processor = Select-String "$TagsDir\$ServiceTag.txt" -Pattern 'Processor'
$Processor = "$Processor" -match '<td class="([^"]+)" valign="top">([^"]+)</td><td class="([^"]+)" valign="top"></td><td class="([^"]+)" valign="top">Processor,(.*?),([^"]+)</td>'
$ProcessorModel = $Matches[6]
$ProcessorModel = $ProcessorModel.Replace(" ","")
$ProcessorModel = $ProcessorModel.Replace("</td></tr></table>","")
$ProcessorCount = $Matches[2]
$ProcessorSize = $Matches[5]
$Memory = Select-String "$TagsDir\$ServiceTag.txt" -Pattern 'Memory'
$Memory = "$Memory" -match '<td class="([^"]+)" valign="top">([^"]+)</td><td class="([^"]+)" valign="top"></td><td class="([^"]+)" valign="top">Dual In-line Memory Module,(.*?),([^"]+)</td>'
$MemoryModel = $Matches[6]
$MemoryModel = $MemoryModel.Replace(" ","")
$MemoryModel = $MemoryModel.Replace("</td></tr></table>","")
$MemoryCount = $Matches[2]
$MemorySize = $Matches[5]
$Raid = Select-String "$TagsDir\$ServiceTag.txt" -Pattern 'Assembly'
$Raid = "$Raid" -match '<td class="([^"]+)" valign="top">([^"]+)</td><td class="([^"]+)" valign="top"></td><td class="([^"]+)" valign="top">Assembly,Cable,(.*?),([^"]+)</td>'
$RaidModel = $Matches[5]
$RaidModel = $RaidModel.Replace(" ","")
$RaidModel = $RaidModel.Replace("</td></tr></table>","")
$RaidCount = $Matches[2]
$RaidType = $Matches[6]
Write-Host Service Tag: $ServiceTag -ForegroundColor Red
Write-Host Hard Drive: -ForegroundColor Green
write-host Adet: $HardDriveCount
write-host Kapasite: $HardDriveSize
write-host Model: $HardDriveModel
Write-Host Processor: -ForegroundColor Green
write-host Adet: $ProcessorCount
write-host Kapasite: $ProcessorSize
write-host Model: $ProcessorModel
Write-Host Memory: -ForegroundColor Green
write-host Adet: $MemoryCount
write-host Kapasite: $MemorySize
write-host Model: $MemoryModel
Write-Host Raid: -ForegroundColor Green
write-host Adet: $RaidCount
write-host Tip: $RaidType
write-host Model: $RaidModel
write-host
}
You can use Add-Content to print them to txt files.
Posted in Exchange Server, Windows Powershell | No Comment | 4,131 views | 05/05/2010 19:59
Exchange Server 2010 kurulumu sonrası gelen default mailbox’ı, içinde sistem mailbox’ı barındırdığı için EMC üzerinden Remove edemezsiniz. Bunun için Powershell üzerinden aşağıdaki adımları uygulamanız gerekiyor.
Öncelikle gizli sistem mailbox’larını görüntüleyelim:
Sonra aşağıdaki komut ile bu mailbox’ları yeni database’e taşıyalım:
Get-Mailbox -Arbitration | New-MoveRequest -TargetDatabase “Yeni_Database” |
Get-Mailbox -Arbitration | New-MoveRequest -TargetDatabase “Yeni_Database”
Yukardaki komut sonrası bu mailbox’lar taşınmaya başlarlar. Taşınma durumunu aşağıdaki komut ile görebilirsiniz.
Eğer mailbox’ların taşınma işlemi için completed deniliyorsa, aşağıdaki komut ile bu mailbox’ların MoveRequest’lerini kaldırabilirsiniz.
Get-MoveRequest | Remove-MoveRequest |
Get-MoveRequest | Remove-MoveRequest
Bu işlemler sonrasında artık EMC üzerinden eski database’i kaldırmanız mümkün durumdadır.
Posted in Exchange Server, Windows Powershell | No Comment | 3,701 views | 05/05/2010 00:00
Yapınızda birden fazla Exchange Server 2010 var ise aşağıdaki gibi csr başvurusu yapabilir ve aynı SSL’i birden fazla sunucu üzerinde tutabilirsiniz.
New-ExchangeCertificate -FriendlyName 'exchange.radore.net' -GenerateRequest -PrivateKeyExportable $true -KeySize '2048' -SubjectName 'C=TR,S="Metrocity",L="Istanbul",O="Radore Hosting",OU="IT",CN=exchange.radore.net' -DomainName 'exchange.radore.net','autodiscover.rh.com.tr','autodiscover.rh.net.tr','EXCAS','MAILBOX','EXC2010' -Server 'EXCAS' |
New-ExchangeCertificate -FriendlyName 'exchange.radore.net' -GenerateRequest -PrivateKeyExportable $true -KeySize '2048' -SubjectName 'C=TR,S="Metrocity",L="Istanbul",O="Radore Hosting",OU="IT",CN=exchange.radore.net' -DomainName 'exchange.radore.net','autodiscover.rh.com.tr','autodiscover.rh.net.tr','EXCAS','MAILBOX','EXC2010' -Server 'EXCAS'
Sertifikanın complete işlemini Exchange arabirimi üzerinden yapabilirsiniz.
Posted in Exchange Server, Windows Powershell | No Comment | 5,898 views | 27/04/2010 22:05
Exchange 2007’den mailbox sildiğinizde, bu mailbox disconnected mailbox’lar altında görünmeye devam eder. Mailbox’ın kullanıcısını ise başka bir mailbox ekleyemezsiniz bu durumda. Bu yüzden disconnected mailbox’ı kaldırmalı ve kullanıcıyı boşa çıkarmalıyız. Bu işlemi de Powershell üzerinden yapabilmekteyiz. Aşağıda Powershell v1 ve v2 kodlarını bulabilirsiniz.
Powershell v1:
Get-MailboxStatistics | where-object { $_.DisconnectDate -ne $null } | Select DisplayName,MailboxGuid |
Get-MailboxStatistics | where-object { $_.DisconnectDate -ne $null } | Select DisplayName,MailboxGuid
Powershell v2:
Get-MailboxStatistics -Database "DBName" | where-object { $_.DisconnectDate -ne $null } | Select DisplayName,MailboxGuid |
Get-MailboxStatistics -Database "DBName" | where-object { $_.DisconnectDate -ne $null } | Select DisplayName,MailboxGuid
Yukardan MailboxGuid’i aldıktan sonra aşağıdaki komut ile silme işlemini gerçekleştirebilirsiniz.
Remove-Mailbox -Database <Database-Name> -StoreMailboxIdentity <MailboxGuid> -confirm:$false |
Remove-Mailbox -Database <Database-Name> -StoreMailboxIdentity <MailboxGuid> -confirm:$false
Bu işlem sonrası kullanıcınıza sıfır bir mailbox bağlayabilirsiniz.
Posted in Hosting & IIS7, Windows Powershell | No Comment | 11,036 views | 18/04/2010 12:05
First, you need IIS Log Parser 2.2 to parse log files and calculate daily usage. Then we will get list of websites with Powershell and write bandwidth usages into a file.
Download IIS Log Parser v2.2:
Install IIS Log Parser v2.2. I created a new directory in drive D called LogParser and copied 2 files (logparser.dll and logparser.exe) into that directory. Now let’s see Powershell codes how to use LogParser v2.2 with Powershell.
1
2
3
4
5
6
7
8
9
10
11
12
| Import-Module WebAdministration
Websites = Get-Website *
Foreach ($i in $Websites)
{
$Name = $i.Name
$ID = $i.ID
$PhysicalPath = $i.PhysicalPath
$Date = (Get-Date).AddDays(-1).ToString("yyMMdd") + '.log'
$Bandwidth = &'D:\tools\LogParser\LogParser.exe' "Select Div(Sum(sc-bytes),1048576) As Bandwidth From '$PhysicalPath\W3SVC$ID\u_ex$Date'" -q:ON
$Value = "$Name : $Bandwidth"
Add-Content -Path Bandwidth.txt -Value $Value
} |
Import-Module WebAdministration
Websites = Get-Website *
Foreach ($i in $Websites)
{
$Name = $i.Name
$ID = $i.ID
$PhysicalPath = $i.PhysicalPath
$Date = (Get-Date).AddDays(-1).ToString("yyMMdd") + '.log'
$Bandwidth = &'D:\tools\LogParser\LogParser.exe' "Select Div(Sum(sc-bytes),1048576) As Bandwidth From '$PhysicalPath\W3SVC$ID\u_ex$Date'" -q:ON
$Value = "$Name : $Bandwidth"
Add-Content -Path Bandwidth.txt -Value $Value
}
You see how easy it is? But this is just an example. I don’t care about Log Path. You should edit path for your own environment. Have fun!
|