Couple of week back, I was playing around with using Powershell for some simple activities. The fall out of that was the ability to search the SQL Server Error Logs. Though we can search for anything, I just showed you a case of usage. In this blog post, I will talk about one of the most sought after request for searching what the disk latencies are. Let us see a powershell script for for Identify Disk Latency on SQL Server Box.
Disks are one of the most notorious when it comes to bring the server performance down. It is sometimes quite difficult to identify the problems. A lot of times, the first point of search would be using resource monitor. But in this blog, I thought let us try to automate this collection process using a PowerShell script. This ultimately shows how powerful this scripting language is and how one can use it to collect and process few Performance Counter values.
param ( [string]$ServerName = "localhost", [int]$SampleFrequencySeconds = 10, [int]$CollectionDurationSeconds = 120 ) # do a check to ensure that collection duration is greater than # or equal to the frequency rate if ($CollectionDurationSeconds -lt $SampleFrequencySeconds) { Write-Error "CollectionDurationSeconds cannot be less than SampleFrequencySeconds" exit } # loop through all of the drives, sampling them $DrivesOutput = for ($i = 0; $i -lt [int]($CollectionDurationSeconds / $SampleFrequencySeconds); $i++) { Get-Counter -Counter "\LogicalDisk(*)\avg. disk sec/transfer" | Select-Object -ExpandProperty CounterSamples | Where-Object {$_.InstanceName -ne "_total"} | Select-Object InstanceName, @{Name = "Type"; Expression = {"LOGICAL"}}, CookedValue Get-Counter -Counter "\PhysicalDisk(*)\avg. disk sec/transfer" | Select-Object -ExpandProperty CounterSamples | Where-Object {$_.InstanceName -ne "_total"} | Select-Object InstanceName, @{Name = "Type"; Expression = {"PHYSICAL"}}, CookedValue # Sleep for the specified frequency before continuing in the loop Start-Sleep -Seconds $SampleFrequencySeconds } # Group by the drive and Calculate the average for each drive we have # round to the nearest [ms] $DrivesOutput | Group-Object InstanceName, Type | Select-Object @{Name = "InstanceName"; Expression = {$_.Group.InstanceName[0]}}, @{Name = "Type"; Expression = {$_.Group.Type[0]}}, @{Name = "DiskLatencyMs"; Expression = { [int](($_.Group.CookedValue | Measure-Object -Average).Average * 1000)} } | Sort-Object InstanceName</pre> <pre>
As part of the script we have gone ahead and started our collection of counters once every 10 seconds. In this script, I have used 2 mins interval for the collection process. We have collected two counters – “\LogicalDisk(*)\avg. disk sec/transfer” and “\PhysicalDisk(*)\avg. disk sec/transfer”.
I have gone ahead and shown a typical output from my PC. I have two physical drives and 3 logical drives. And we can see the disk latencies in (milliseconds).
I am sure the output would vary in your environments. Do let me know if such scripts are useful and you would want me to write more about them. I am slowly starting to love this scripting language that I plan to explore the various ways people use this powerful features.
Do let me know via comments some of the ways you have used PowerShell in your environments and which of those are even used in Production?
Reference: Pinal Dave (https://blog.sqlauthority.com)