了解 VHD 磁盘压缩使用情况和性能

可以使用 Windows 事件日志了解使用 VHD 磁盘压缩的频率、已保存的空间和运行时间。 下面是一些示例 PowerShell 脚本和 Azure Log Analytics 查询,可用于帮助解释事件。

PowerShell

VHD 磁盘压缩指标脚本

此示例使用 PowerShell 从前 30 天获取格式化为网格的 VHD 磁盘压缩事件。 在提升的 PowerShell 提示符下,运行以下代码块:

# Set startTime to number of days to search the event logs
$startTime = (Get-Date).AddDays(-30)

# Query Event Log using Get-WinEvent filtered to the VHD Disk Compaction metric events
$diskCompactionEvents = Get-WinEvent -FilterHashtable @{
    StartTime       = $startTime
    ProviderName    = 'Microsoft-FSLogix-Apps'
    ID         = 57
}

# Format event properties
$compactionMetrics = $diskCompactionEvents | Select-Object `
    @{l="Timestamp";e={$_.TimeCreated}},`
    @{l="ComputerName";e={$_.MachineName}},`
    @{l="Path";e={$_.Properties[0].Value}},`
    @{l="WasCompacted";e={$_.Properties[1].Value}},`
    @{l="TimeSpent(sec)";e={[math]::round($_.Properties[7].Value / 1000,2)}},`
    @{l="MaxSize(GB)";e={[math]::round($_.Properties[2].Value / 1024,2)}},`
    @{l="MinSize(GB)";e={[math]::round($_.Properties[3].Value / 1024,2)}},`
    @{l="InitialSize(GB)";e={[math]::round($_.Properties[4].Value / 1024,2)}},`
    @{l="FinalSize(GB)";e={[math]::round($_.Properties[5].Value / 1024,2)}},`
    @{l="SavedSpace(GB)";e={[math]::round($_.Properties[6].Value / 1024,2)}}

# Display metrics in Out-GridView
$compactionMetrics | Out-GridView

Azure Log Analytics 查询

重要

若要使用以下查询,必须先将虚拟机配置为将其事件日志发送到 Log Analytics 工作区。 有关详细信息,请参阅 使用 Log Analytics 代理收集 Windows 事件日志数据源。 用于 VHD 磁盘压缩的日志包括:

  • Microsoft-FSLogix-Apps/Operational
  • Microsoft-FSLogix-Apps/Admin

VHD 磁盘压缩指标查询

在 VHD 磁盘压缩操作期间花费的时间

显示压缩操作期间花费的平均、最小和最长时间。 根据磁盘是否能够压缩来汇总数据。

Event
| where EventLog == 'Microsoft-FSLogix-Apps/Operational' and EventID == 57
| parse kind=relaxed EventData with *
    "<Data Name=\"Path\">" Path
    "</Data><Data Name=\"WasCompacted\">" DiskCompaction
    "</Data><Data Name=\"MaxSupportedSizeMB\">" MaxSupportedSizeMB
    "</Data><Data Name=\"MinSupportedSizeMB\">" MinSupportedSizeMB
    "</Data><Data Name=\"SizeBeforeMB\">" SizeBeforeMB
    "</Data><Data Name=\"SizeAfterMB\">" SizeAfterMB
    "</Data><Data Name=\"SavedSpaceMB\">" SavedSpaceMB
    "</Data><Data Name=\"TimeSpentMillis\">" TimeSpentMillis "</Data>" *
| extend TimeSpent = todecimal(TimeSpentMillis) / 1024
| where DiskCompaction <> ""
| summarize Average=round(avg(TimeSpent),2), Max=round(max(TimeSpent),2), Min=round(min(TimeSpent),2) by DiskCompaction

下面是输出示例:

A bar chart showing the result of running the Time Spent query

压缩的容器 VHD(x) 文件数

显示根据阈值选择多少个容器 VHD(x) 文件进行压缩。

Event
| where EventLog == 'Microsoft-FSLogix-Apps/Operational' and EventID == 57
| parse kind=relaxed EventData with *
    "<Data Name=\"Path\">" Path
    "</Data><Data Name=\"WasCompacted\">" DiskCompaction
    "</Data><Data Name=\"MaxSupportedSizeMB\">" MaxSupportedSizeMB
    "</Data><Data Name=\"MinSupportedSizeMB\">" MinSupportedSizeMB
    "</Data><Data Name=\"SizeBeforeMB\">" SizeBeforeMB
    "</Data><Data Name=\"SizeAfterMB\">" SizeAfterMB
    "</Data><Data Name=\"SavedSpaceMB\">" SavedSpaceMB
    "</Data><Data Name=\"TimeSpentMillis\">" TimeSpentMillis "</Data>" *
| where DiskCompaction <> ""
| summarize NumberOfVhdContainers=count() by DiskCompaction

下面是输出示例:

A pie chart showing the number of V H D files (containers) compacted

已保存总存储空间

显示 VHD 磁盘压缩操作期间回收的以 GB 为单位的存储量。

Event
| where EventLog == 'Microsoft-FSLogix-Apps/Operational' and EventID == 57
| parse kind=relaxed EventData with *
    "<Data Name=\"Path\">" Path
    "</Data><Data Name=\"WasCompacted\">" DiskCompaction
    "</Data><Data Name=\"MaxSupportedSizeMB\">" MaxSupportedSizeMB
    "</Data><Data Name=\"MinSupportedSizeMB\">" MinSupportedSizeMB
    "</Data><Data Name=\"SizeBeforeMB\">" SizeBeforeMB
    "</Data><Data Name=\"SizeAfterMB\">" SizeAfterMB
    "</Data><Data Name=\"SavedSpaceMB\">" SavedSpaceMB
    "</Data><Data Name=\"TimeSpentMillis\">" TimeSpentMillis "</Data>" *
| extend Storage = todecimal(SavedSpaceMB)
| summarize StorageSavings = (format_bytes(sum(Storage * 1024 * 1024),2,"GB"))

Winlogon (注销) 延迟警告

显示导致 Winlogon 超过 60 秒阈值的任何服务。 显示出现的次数以及花费的平均时间和最长时间。

Event
| where Source == 'Microsoft-Windows-Winlogon' and EventID == 6006
| parse kind=relaxed ParameterXml with "<Param>" ServiceName "</Param><Param>" Duration "</Param><Param>" EventType "</Param><Param>-</Param>"
| extend TimeInSeconds = todecimal(Duration)
| where EventType == "Logoff"
| summarize Occurrences=count(),Average=round(avg(TimeInSeconds),2), Minimum=round(min(TimeInSeconds),2), Maximum=round(max(TimeInSeconds),2) by ServiceName

下面是输出示例:

A table showing the services which exceeded the Winlogon threshold