未加星标

Get-Counter: Use PowerShell to Get Hyper-V Performance Data

字体大小 | |
[系统(windows) 所属分类 系统(windows) | 发布者 店小二04 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

In my lastarticle, I left you with some PowerShell technique for getting Hyper-V performance counters from windows Management Instrumentation (WMI) using Get-CimInstance . I’ll admit that getting the information from WMI is bit advanced. But PowerShell has another cmdlet called Get-Counter that might be able to simplify the process. Although I expect you’ll want to massage this output as well. Don’t worry. I’ve got plenty of code examples to get you started.

The code I’m going to show you should work against a remote server. Although as a last resort you could run my commands in a remote session using Invoke-Command . To make things easy for you to follow along, I’m going to set a variable for the computer I want to query, just as I did in previous articles. In this case, this will be my Windows 10 laptop running Hyper-V.

$computer = $env:COMPUTERNAME

The Get-Counter cmdlet can list all available counter sets. For our purposes naturally, we are interested in Hyper-V counters.

How to use Get-Counter cmdlet get-counter -listset 'Hyper-V*' -ComputerName $computer
Get-Counter: Use PowerShell to Get Hyper-V Performance Data

In my setup, I am always most concerned about memory usage since that is the one resource that is always limited for me. So I only care about dynamic memory


Get-Counter: Use PowerShell to Get Hyper-V Performance Data

The key bit of information is the list of counters. These are the things I can get data on. Here’s the really cool part. I can pipe this output back to Get-Counter and get results for all available counters and instances.

$m = get-counter -listset 'Hyper-V Dynamic Memory VM' -ComputerName $computer | get-counter

I saved the results to a variable so I can re-use it.


Get-Counter: Use PowerShell to Get Hyper-V Performance Data

The output of Get-Counter is great to look at but a bit cumbersome. The CounterSamples property lists the counter and value. In the value, you can see the computername, (YPJH10) and the virtual machine name like SRV1 and DOM1. You may prefer a better look at this data.

$m.CounterSamples | sort InstanceName,Path | format-table -GroupBy InstanceName -property Path,Cookedvalue,timestamp

Here I’ve created a formatted table grouped by each instance name.


Get-Counter: Use PowerShell to Get Hyper-V Performance Data

The Path values are a bit redundant so I’ll clean them up a bit with the help of a regular expression.

[regex]$rx = "(?<=\\\\).*(?=\\)" $m.CounterSamples | sort InstanceName,Path | format-table -GroupBy InstanceName -property @{Name="Counter";Expression={Split-path $_.path -Leaf}}, Cookedvalue,timestamp,@{Name="VMHost";Expression={$rx.Match((Split-Path $_.path)).value.ToUpper()}}

( can get the counter itself by splitting the path to get the last part. The regular expression pulls out the Hyper-V hostname from between the slashes.


Get-Counter: Use PowerShell to Get Hyper-V Performance Data

This is a bit prettier and easier to read, but I can’t do much with it other than sending to a file or printer. But since performance counters are about reporting anyway that doesn’t matter a great deal. In fact, I know how much you all love HTML reports so here’s something you can use to create another version of this information.

#requires -version 5.0 #requires -runasAdministrator #HVDynamicMemoryReport.ps1 [cmdletbinding()] Param( [Parameter(Position = 0, HelpMessage = "Enter the name of the Hyper-V Host")] [ValidateNotNullOrEmpty()] [string]$Computername = $env:COMPUTERNAME, [Parameter(HelpMessage = "Enter the filename for the html report")] [ValidateNotNullOrEmpty()] [ValidateScript( {Test-Path (Split-Path $_)})] [string]$Path = "$env:temp\dynamicmemoryreport.htm" ) #a meta data value used in the html footer $scriptVersion = "1.1" [regex]$rx = "(?<=\\\\).*(?=\\)" $Counters = get-counter -listset 'Hyper-V Dynamic Memory VM' -ComputerName $computername | get-counter $data = $counters.CounterSamples | Sort-Object -property InstanceName, Path | Group-Object -Property InstanceName $fragments = @("<H1>Dynamic Memory Performance</H1>") $fragments += "<H2>Hyper-V Host: $($Computername.toUpper())</h2>" foreach ($item in $data) { $fragments += "<h3>$($item.name.toupper())</h3>" $fragments += $item.group | Select-object -Property @{Name = "Counter"; Expression = {Split-path $_.path -Leaf}}, Cookedvalue, Timestamp | Sort-Object -Property Counter | ConvertTo-Html -Fragment -as Table } #define a here string for the html header with embedded style sheet $head = @" <title>Dynamic Memory Performance $($computername.toupper())</title> <style> body { background-color:#FAFAFA; font-family:Arial; font-size:12pt; } td, th { border:1px solid black; border-collapse:collapse; } th { color:white; background-color:black; } table, tr, td, th { padding: 0; margin: 0} tr:nth-child(odd) { background-color: lightgray } table { margin-left:50px; } img { float:left; margin: 0px 25px; } .footer { font-size:8pt; } .footer tr:nth-child(odd) {background-color: white} .footer td,tr { border-collapse:collapse; border:none; } </style> "@ #HTML to display at the end of the report [xml]$meta = [pscustomobject]@{ Date = Get-Date Author = "$env:USERDOMAIN\$env:username" Script = $($myinvocation.invocationname) Version = $scriptVersion Source = $($Env:COMPUTERNAME) } | ConvertTo-Html -Fragment -as List $class = $meta.CreateAttribute("class") $meta.table.SetAttribute("class","footer") $footer = @" <br> <i> $($meta.innerxml) </i> "@ #create the HTML document ConvertTo-HTML -Head $head -Body $fragments -PostContent $footer | Out-File -FilePath $path -Encoding ascii Write-Host "Report saved to $path" -ForegroundColor Green

This script is designed to report on the Hyper-V dynamic memory counters, but you could modify it for probably any counter set. You could also modify it to limit reporting to a specific virtual machine by inserting some code to filter on the Instance. This script includes a CSS style sheet embedded into the report that will display table rows in alternate colors.


Get-Counter: Use PowerShell to Get Hyper-V Performance Data

I think by now your head is spinning with PowerShell code so I’ll wrap it up for now so you have some time to try these things out. I hope you are finding these excursions into performance counters with PowerShell useful because I have a few more things in mind to share with you. In the meantime kick the samples around, in a test environment of course, and let me know what you think in the comments section below!

Want to learn more neat tips using PowerShell? Check out these 5 PowerShell Hacks for Hyper-V!

Are you having trouble getting your PowerShell scripts to run properly? Find out Why Your Hyper-V PowerShell Commands Don’t Work (and how to fix them)

本文系统(windows)相关术语:三级网络技术 计算机三级网络技术 网络技术基础 计算机网络技术

代码区博客精选文章
分页:12
转载请注明
本文标题:Get-Counter: Use PowerShell to Get Hyper-V Performance Data
本站链接:https://www.codesec.net/view/610943.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(windows) | 评论(0) | 阅读(104)