当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 创建 Hyper-V VM 内存报告

作者:精品下载站 日期:2024-12-14 07:46:20 浏览:14 分类:玩电脑

创建 Hyper-V VM 内存报告


我使用 Hyper-V 来运行我的实验室环境。由于我在家工作,无法访问“真实”的生产网络,因此我必须使用虚拟化环境。考虑到预算限制,我也没有大量具有无限内存和存储空间的高端硬件。因此,我经常使用最低限度的内存来运行虚拟机。大多数时候这不是问题。不过,有时我需要快速查看我用了多少内存。我可以使用 Get-VMMemory 或 Get-VM cmdlet。

[玩转系统] 创建 Hyper-V VM 内存报告

后一个 cmdlet 包含更多细节。

[玩转系统] 创建 Hyper-V VM 内存报告

所有值均以字节为单位,我知道我可以使用自定义哈希表将其转换为 MB。但我不想一直这样做,因此我创建了一个命令来获取详细的虚拟机内存。

#requires -version 4.0
#requires -module Hyper-V

Function Get-VMMemoryReport {
<#
.Synopsis
Get a VM memory report.
.Description
This command gets memory settings for a given Hyper-V virtual machine. All memory values are in MB. The Utilization value is the percentage of assigned memory is use or in demand.

The command requires the Hyper-V module and must be run in an elevated PowerShell session.
.Parameter VMName
The name of the virtual machine or a Hyper-V virtual machine object. This parameter has an alias of "Name." 
.Parameter VM
A Hyper-V virtual machine object. See examples.
.Parameter Low
Filter out virtual machines without memory issues. Only get virtual machines with a Low memory status.
.Parameter Computername
The name of the Hyper-V server to query. The default is the local host. The parameter has an alias of CN.
.Example
PS C:\> Get-VMMemoryReport chi-core01 -ComputerName chi-hvr2 

Computername : CHI-HVR2
Name         : CHI-CORE01
Status       : Low
Dynamic      : True
Assigned     : 514
Demand       : 472
Utilization  : 91.83
Startup      : 512
Minimum      : 512
Maximum      : 1024
Buffer       : 20
Priority     : 50


Get a memory report for a single virtual machine.
.Example
PS C:\> Get-VM -computer chi-hvr2 | where {$_.state -eq 'running'} | Get-VMMemoryReport | Sort Status,Name | Out-Gridview -title "Memory Report"

Display a memory report for all running VMs using Out-Gridview.
.Example
PS C:\> get-vmmemoryreport -low -cn chi-hvr2 | format-table Name,Assigned,Demand,Utilization,Maximum

Name       Assigned Demand Utilization Maximum
----       -------- ------ ----------- -------
CHI-SQL01      2586   2534       97.99    4096
CHI-FP02        846    812       95.98    2048
CHI-CORE01      514    483       93.97    1024

Get virtual machines with a low memory status.
.Example
PS C:\> get-content d:\MyVMs.txt | get-vmmemoryreport | Export-CSV c:\work\VMMemReport.csv -notypeinformation
Get virtual machine names from the text file MyVMs.txt and pipe them to Get-VMMemoryReport. The results are then exported to a CSV file.
.Example
PS C:\> get-vm -computer chi-hvr2 | get-vmmemoryreport | Sort Maximum | convertto-html -title "VM Memory Report" -css c:\scripts\blue.css -PreContent "<H2>Hyper-V Memory Report</H2>" -PostContent "<br>An assigned value of 0 means the virtual machine is not running." | out-file c:\work\vmmemreport.htm
Get a memory report for all virtual machines, sorted on the maximum memory property. This command then creates an HTML report.

.Notes
Last Updated: July 20, 2015
Version     : 3.0

.Link
Get-VM
Get-VMMemory
.Inputs
String
Hyper-V virtual machine
.Outputs
Custom object
#>

[cmdletbinding(DefaultParameterSetName="Name")]
Param(
[Parameter(Position=0,HelpMessage="Enter the name of a virtual machine",
ValueFromPipeline,ValueFromPipelineByPropertyName,
ParameterSetName="Name")]
[alias("Name")]
[ValidateNotNullorEmpty()]
[string]$VMName="*",

[Parameter(Position=0,Mandatory,HelpMessage="Enter the name of a virtual machine",
ValueFromPipeline,ValueFromPipelineByPropertyName,
ParameterSetName="VM")]
[ValidateNotNullorEmpty()]
[Microsoft.HyperV.PowerShell.VirtualMachine[]]$VM,

[switch]$Low,

[ValidateNotNullorEmpty()]
[Parameter(ValueFromPipelinebyPropertyName)]
[ValidateNotNullorEmpty()]
[Alias("CN")]
[string]$Computername=$env:COMPUTERNAME
)

Begin {
    Write-Verbose "Starting $($MyInvocation.Mycommand)"  
    #initialize an array to hold results
    $data = @()
} #begin

Process {

    if ($PSCmdlet.ParameterSetName -eq "Name") {
        Try {
            $VMs = Get-VM -name $VMName -ComputerName $computername -ErrorAction Stop
        }
        Catch {
            Write-Warning "Failed to find VM $vmname on $computername"
            #bail out
            Return
        }
    }
    else {
         $VMs = $VM
    }

    foreach ($V in $VMs) {
    #get memory values
    Try {
        Write-Verbose "Querying memory for $($v.name) on $($computername.ToUpper())"
        $memorysettings = Get-VMMemory -VMName $v.name  -ComputerName $Computername -ErrorAction Stop

    if ($MemorySettings) {
        #calculate memory utilization if VM is running
        if ($v.State -eq 'running') {
            #calculate % to 2 decimal points
            $util = [math]::Round(($v.MemoryDemand/$v.MemoryAssigned)*100,2)
        }
        else {
            $util = 0
        }    
        #all values are in MB
        $hash=[ordered]@{
            Computername = $v.ComputerName.ToUpper()
            Name = $V.Name
            Status = $v.memoryStatus
            Dynamic = $V.DynamicMemoryEnabled
            Assigned = $V.MemoryAssigned/1MB
            Demand = $V.MemoryDemand/1MB
            Utilization = $util
            Startup = $V.MemoryStartup/1MB
            Minimum = $V.MemoryMinimum/1MB
            Maximum = $V.MemoryMaximum/1MB
            Buffer =  $memorysettings.buffer
            Priority = $memorysettings.priority
        }
    
        #write the new object to the pipeline
        $data+= New-Object -TypeName PSObject -Property $hash
    } #if $memorySettings found
    } #Try
    Catch {
        Throw $_
    } #Catch
    } #foreach $v in $VMs
} #process
End {
    if ($Low) {
        Write-Verbose "Writing Low memory status objects to the pipeline"
        $data.where({$_.status -eq 'Low'})
    }
    else {
        Write-Verbose "Writing all objects to the pipeline"
        $data
    }
    Write-Verbose "Ending $($MyInvocation.Mycommand)"
} #end
} #end Get-VMMemoryReport

#set an alias
Set-Alias -name gvmr -Value Get-VMMemoryReport

我在过去几年中发布了此函数的版本,因此您可能遇到过早期的迭代。此版本的主要变化是我正在计算内存需求与分配的利用率百分比。我还添加了一个开关,以仅显示具有低内存状态的虚拟机,因为这通常是我想知道的最重要的事情。

现在我可以轻松获取单个虚拟机的信息

[玩转系统] 创建 Hyper-V VM 内存报告

或多个:

Get-VMMemoryReport -Computername chi-hvr2 -low | out-gridview -title "Low Memory"

[玩转系统] 创建 Hyper-V VM 内存报告

显然我的 SQL Server 需要一些关注。

我希望你能尝试一下并让我知道你的想法。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯