ขอนำเสนอ Powershell Script สำหรับใช้ Monitor Server Performance สำหรับเครื่อง Windows 2008 R2 ขึ้นไปดังนี้
การติดตั้ง PowerShell Editor และ วิธีใช้งานบน Windows 2008
1. ติดตั้งโดยไปที่
Server Manager -> Features -> Add Features
2. เลือกหัวข้อ
Windows PowerShell Integrated Script Environment (ISE)
3. ให้เลือกใช้งาน Version X86 ซึ่งเมื่อลงแล้ว Icon จะอยู่ที่
Accessories -> Windows PowerShell -> Windows PowerShell ISE (x86)
คำสั่งในการเปิด Execution Policy
Set-ExecutionPolicyUnrestricted -Force
การสร้าง Loop เพื่อให้สามารถรันได้ตลอดเวลา
While($true){
.....[Monitor Script].....
}
คำสั่งตรวจหา IP ของ Server
Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object { $_.Description -eq "Intel(R) Dual Band Wireless-N 7260" } | ForEach-Object { $ipaddr = $_.IPAddress }
$ip = $ipaddr[0]
คำสั่งตรวจสอบ Max CPU Clock Speed
Get-WmiObject-class win32_processor -Property "maxclockspeed" | ForEach-Object {$clockspeed = $_.maxclockspeed }
Get-WmiObject-class win32_computersystem -Property "NumberOfLogicalProcessors" |
ForEach-Object { $lprocessor = $_.NumberOfLogicalProcessors }
$sumclockspeed = $clockspeed*$lprocessor
คำสั่งตรวจสอบ Current CPU
(Get-Counter "\Processor(_Total)\% Processor Time").CounterSamples | ForEach-Object {$cpuuse = $_.CookedValue}
$cpuuse = [math]::round($cpuuse,0)
$clockspeed = ($cpuuse*$sumclockspeed)/100
$clockspeed = [math]::round($clockspeed,0)
$pcpuuse = $clockspeed/$sumclockspeed
$pcpuuse = [math]::round($pcpuuse,0)
คำสั่งตรวจสอบ Maximum Memory
$sumram = 0
Get-WMIObject-class win32_physicalmemory -Property "Capacity" | ForEach-Object { $sumram = $sumram + $_.Capacity}
$sumram = $sumram/(1024*1024)
คำสั่งตรวจสอบ Current Memory
(Get-Counter "\Memory\Available MBytes").CounterSamples | ForEach-Object {$memfree = $_.CookedValue}
$memuse = $sumram-$memfree;
$pmemuse = ($memuse/$sumram)*100
$pmemuse = [math]::round($pmemuse,0)
คำสั่งตรวจสอบ Connection
$netstat = "netstat -na"
$netstatvalue = invoke-expression -command "$netstat" | where
{$_ -match "ESTABLISHED"}
$port80 = [string]::join(":",($ip,"80"))
$port443 = [string]::join(":",($ip,"443"))
#Connection
$conn = (invoke-expression -command "$netstat" | where {$_ -match "ESTABLISHED"} | Measure-Object -Line).Lines
#Unique IP
if($netstatvalue){
$uniqip = ($netstatvalue | where {$_ -match $port80 -or
$_ -match $port443 } | %{ $_.Split(' ',[StringSplitOptions]'RemoveEmptyEntries')[2] } | %{ $_.Split(':')[0] } | Sort-Object -Unique | Measure-Object -Line).Lines
}else{
$uniqip = 0
}
ตัวอย่างวิธีการ Display Stat บน Powershell Console
cls
Write-Host “ Token : "$token
Write-Host " Server IP : "$ip
Write-Host " TimeStamp : "$timestamp
อาจจะยังไม่ได้อธิบายวิธีการทำ Service Process หรือในส่วนของการตั้ง Task Manager เพื่อทำระบบ Check อัตโนมัติ ไว้ว่าง ๆ เดี๋ยวค่อยมาเขียนต่อให้ครับ