{"id":13201,"date":"2011-07-26T00:01:00","date_gmt":"2011-07-26T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/07\/26\/import-counters-from-a-perfmon-chart-into-powershell\/"},"modified":"2011-07-26T00:01:00","modified_gmt":"2011-07-26T00:01:00","slug":"import-counters-from-a-perfmon-chart-into-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/import-counters-from-a-perfmon-chart-into-powershell\/","title":{"rendered":"Import Counters from a Perfmon Chart into PowerShell"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to automatically import performance counters from a Perfmon chart into Windows PowerShell for ease of analysis.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/>Hey, Scripting Guy! I have several custom Perfmon charts set up. Like many network administrators, I spent a decent amount of time setting up this chart. I would like to see if there is a way I can automatically import those counters into a Windows PowerShell script. It may sound like a goofy request, but if you saw the actual number of charts and counters, this would save me a <i>huge<\/i> amount of time. I am in the same hemisphere as you, which means it is summer: I would rather spend my time playing golf than copying Perfmon counters into Windows PowerShell scripts. My golf handicap needs your assistance. Please!<\/p>\n<p>&mdash;YM<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/>Hello YM,<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. As someone who used to be a scratch golfer (at least when playing a golf simulator on my computer), I sympathize with your predicament. Actually, as someone who absolutely despises rework, I really really sympathize with your situation. I would almost be willing to say, dude (or dudette as the case may be), but you will gain so much more in productivity if you convert your Perfmon charts to Windows PowerShell scripts that it will actually be worth the effort.<\/p>\n<p>Before I get too carried away, I need to tell you a little bit about the Performance Monitor tool. Perfmon is one of my favorite Microsoft tools. I fell in love with it when I was writing a chapter for the <i>MSCE for Dummies<\/i> book for the <i>NT 4 in the Enterprise <\/i>exam. I was so excited about the tool, I thought about writing an entire book about the subject; instead, I wrote a book called <i>Network Monitoring and Analysis: A Protocol Approach to Troubleshooting<\/i>.<\/p>\n<p>Unfortunately, there is no really straightforward way to export counters from Perfmon. There are lots of ways to export your settings and various configurations, but no easy way to export only the settings. I will use an example to make this clear.<\/p>\n<p>I have created a custom performance counter set, as is shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7215.HSG-7-26-11-01.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of custom performance counter set\" alt=\"Image of custom performance counter set\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7215.HSG-7-26-11-01.png\" \/><\/a><\/p>\n<p>I can right-click the graph portion and choose <b>Save Counters As<\/b> from the shortcut menu. The <b>Web Page<\/b> option is probably the easiest to work with.<\/p>\n<p>As an aside, I can open the web page in Internet Explorer, click the <b>Unfreeze Display<\/b> button, and start a new real-time trace. It is really cool, and is shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3162.HSG-7-26-11-02.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of clicking Unfreeze Display button\" alt=\"Image of clicking Unfreeze Display button\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3162.HSG-7-26-11-02.png\" \/><\/a><\/p>\n<p>To create a new data collector set from a Performance Monitor graph, right-click <b>Performance Monitor<\/b> in the left pane, and click <b>New Data Collector Set<\/b> in the shortcut menu. All the counters from the graph will automatically appear in the new Data Collector Set.<\/p>\n<p>When I have a custom Data Collector Set, I can start the data collection in the log file that was configured during the creation of the Data Collector Set. I prefer the binary file type (.blg) because it imports easily via the <b>Import-Counter<\/b> cmdlet.<\/p>\n<p>These steps are not necessarily something that every network administrator will need to accomplish. In fact, if you already have custom Performance Monitor charts and Data Collector Sets, there are probably already trace log files that have been created. All I need to pull the counters is a single snapshot.<\/p>\n<p>Now for the fun part.<\/p>\n<p>After I have a .blg<i> <\/i>file, I can use the <b>Import-Counter<\/b> cmdlet to import the log. I can then use normal Windows PowerShell techniques to parse the file and analyze the data. Windows PowerShell makes it easy to look through massive amounts of data and search for anomalies or patterns. But the specific task at hand is to use Windows PowerShell to query the same counters that are defined in the custom Perfmon trace. To do this, I provide the path to the .blg<i> <\/i>file, and I store the returned performance data in a variable. This technique is shown here:<\/p>\n<p style=\"padding-left: 30px\">$counters = Import-Counter -Path &#8220;C:\\Users\\edwilson\\Perf\\System Monitor Log.blg&#8221;<\/p>\n<p>After I have the data stored in a variable, I can explore the contents as illustrated in the following figure.<\/p>\n<\/p>\n<p>It is obvious I am dealing with a collection, so I am able to index directly into the collection by using a square bracket and a number. Each slice will contain the same types of data (of course, the actual values will vary). As a result, I can simply index into the first element by using a zero. This technique is shown here (with truncated output):<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $counters[0].countersamples<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"><span style=\"text-decoration: underline\">Path<\/span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"text-decoration: underline\">InstanceName<\/span>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"text-decoration: underline\">CookedValue<\/span><\/p>\n<p style=\"padding-left: 30px\">\\\\edwils1\\tcpv4\\connection&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fai&#8230;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0<\/p>\n<p style=\"padding-left: 30px\">\\\\edwils1\\tcpv4\\segments\/sec&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0<\/p>\n<p style=\"padding-left: 30px\">&lt;output is truncated &hellip; bigtime &hellip;&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>The property I want is the path to the counters. I can use the <b>Select-Object<\/b> cmdlet to retrieve them and store them into a variable. This technique is shown here:<\/p>\n<p style=\"padding-left: 30px\">$paths = $counters[0].countersamples | % {$_.path}<\/p>\n<p>&nbsp;<\/p>\n<p>Now that I have a collection of paths, I might be inclined to examine them. I can do this by displaying the value of the <b>$paths<\/b> variable. This is shown here with truncated output:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $paths<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"><span style=\"text-decoration: underline\">Path<\/span><\/p>\n<p style=\"padding-left: 30px\">\\\\edwils1\\tcpv4\\connection failures<\/p>\n<p style=\"padding-left: 30px\">\\\\edwils1\\tcpv4\\segments\/sec<\/p>\n<p style=\"padding-left: 30px\">\\\\edwils1\\tcpv4\\connections established<\/p>\n<p style=\"padding-left: 30px\">\\\\edwils1\\tcpv4\\connections reset<\/p>\n<p style=\"padding-left: 30px\">\\\\edwils1\\tcpv4\\segments received\/sec<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">How many paths do I have? I can use the <b>Measure-Object<\/b> cmdlet to find:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $paths | Measure-Object<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Count&nbsp;&nbsp;&nbsp; : 81<\/p>\n<p style=\"padding-left: 30px\">Average&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Sum&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Maximum&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Minimum&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Property :<\/p>\n<p>&nbsp;<\/p>\n<p>The cool thing about the <b>Get-Counter<\/b> cmdlet is I can easily pipe to it. The command and associated output are shown here (output is truncated):<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Counter -Counter $paths<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"><span style=\"text-decoration: underline\">Timestamp<\/span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"text-decoration: underline\">CounterSamples<\/span><\/p>\n<p style=\"padding-left: 30px\">7\/20\/2011 7:34:06 PM&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \\\\edwils1\\network interface(intel[r] 82566mm gigabit network connection)\\<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bytes total\/sec :<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \\\\edwils1\\network interface(local area connection* 9)\\bytes total\/sec :<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \\\\edwils1\\network interface(6to4 adapter)\\bytes total\/sec :<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0<\/p>\n<p>&nbsp;<\/p>\n<p>The complete sequence of commands required to import the performance monitor log, store the results in a variable, choose the path from the first instance of the counter samples, store the resultant custom object in a variable, and pipe the variable to the <b>Get-Counter<\/b> cmdlet to perform a query is shown here:<\/p>\n<p style=\"padding-left: 30px\">$counters = Import-Counter -Path &#8220;C:\\Users\\edwils\\Desktop\\HSG-New\\System Monitor Log.blg&#8221;<\/p>\n<p style=\"padding-left: 30px\">$paths = $counters[0].countersamples | % {$_.path}<\/p>\n<p style=\"padding-left: 30px\">$counterData = Get-Counter -Counter $paths<\/p>\n<p>&nbsp;<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to automatically import performance counters from a Perfmon chart into Windows PowerShell for ease of analysis. &nbsp; Hey, Scripting Guy! I have several custom Perfmon charts set up. Like many network administrators, I spent a decent amount of time setting up this chart. I would like to see if there is a [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[31,60,3,45],"class_list":["post-13201","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-performance","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to automatically import performance counters from a Perfmon chart into Windows PowerShell for ease of analysis. &nbsp; Hey, Scripting Guy! I have several custom Perfmon charts set up. Like many network administrators, I spent a decent amount of time setting up this chart. I would like to see if there is a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=13201"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13201\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=13201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=13201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=13201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}