#requires -version 2.0 <# ----------------------------------------------------------------------------- Script: demo-wmiperf-progress.ps1 Version: 1.0 Author: Jeffery Hicks http://jdhitsolutions.com/blog http://twitter.com/JeffHicks http://www.ScriptingGeek.com Date: 10/26/2011 Keywords: Comments: "Those who forget to script are doomed to repeat their work." **************************************************************** * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * **************************************************************** ----------------------------------------------------------------------------- #> <# .Synopsis Demo WMI Perf Counter Graph .Description This demonstration script uses the Write-Progress cmdlet to display the value of a WMI Performance counter. Ideally, the counter will have a value of between 1 and 100. Otherwise you might need to scale the value. You must specify a WMI Performance counter. The demo sample is: Win32_PerfFormattedData_PerfDisk_LogicalDisk and a filter to limit it to a single instance. The demo filter is: Name='C:' Finally, select a property. The demo property is PercentDiskTime. The demo will check every 1 second (the -Sleep parameter) up to 60 times (the -Maximum parameter). You can also specify the name of a CSV log file to hold the results. #> [cmdletbinding()] Param( [Parameter(Position=0)] [ValidateNotNullorEmpty()] [string]$Class="Win32_PerfFormattedData_PerfDisk_LogicalDisk", [Parameter(Position=1)] [ValidateNotNullorEmpty()] [string]$Filter="Name='C:'", [Parameter(Position=2)] [ValidateNotNullorEmpty()] [string]$Property="PercentDiskTime", [string]$Computername=$env:computername, [int]$Sleep=1, [int]$Maximum=60, [string]$Log ) Write-Verbose ("Querying {0} filter {1} on {2} every {3} second(s) {4} times." -f $class,$filter,$computername,$sleep,$maximum) #values for Write-Progress $activity=$class $status="$Property -> $Filter" $current=$computername.ToUpper() Write-Verbose "Graphing the $Property property" #define an array to hold the results $results=@() for ($i=1; $i -le $maximum; $i++) { Try { $stat=get-wmiobject -class $activity -filter $filter -computername $computername -ErrorAction Stop $results+=$stat Write-progress -Activity $activity -status $status -currentoperation $current -PercentComplete $stat.$Property sleep -seconds $sleep } Catch { Write-Warning $_.Exception.Message Return } } if ($Log) { #export results to a file Write-Verbose "Exporting results to $log" $results | export-csv -Path $log } Write-Progress -Activity $activity -status "Finished" -Completed Write-Verbose "Finished"