文章摘要
这篇文章主要介绍了使用PowerShell脚本对系统计数器数据进行分析和导出的操作方法。脚本的主要功能包括:
1. 定义时间范围(前一日至当前日)
2. 导入指定路径下的计数器数据
3. 遍历每个计数器,提取平均值、最小值和最大值
4. 将结果存储在哈希表中
5. 通过不同的方式导出结果到文件(文本、CSV和Excel格式)
文章通过代码展示了如何高效地处理计数器数据,并将结果以多种格式保存,便于后续分析和使用。
$startDate=(Get-Date).AddDays(-1).Date
$endDate=(Get-Date).Date
$perfPath=”D:\360Downloads\*.blg”
#哈希表存储结果数据
$resultTable=@{}
#导入指定时间的所有计数器信息
$counterData=Import-Counter -Path $perfPath | Where-Object -FilterScript {($_.Timestamp -ge $startDate) -and ($_.Timestamp -lt $endDate)}
#所有的计数器名字
$countersNameList=$counterData[0].countersamples | % {$_.Path}
#遍历每个计数器,将计算结果存储到哈希表中
foreach($counterName in $countersNameList)
{
#$counterName=”\\hzc\system\threads”
$counterDataOne=$counterData | Foreach-Object {$_.CounterSamples} | Where {$_.Path -like $counterName}
$counterInfo=$counterDataOne | Measure-Object CookedValue -Average -Minimum -Maximum
$resultTable.$($counterName+” :平均值”)=$counterInfo.Average
$resultTable.$($counterName+” :最小值”)=$counterInfo.Minimum
$resultTable.$($counterName+” :最大值”)=$counterInfo.Maximum
}
#$resultTable.GetEnumerator() | sort Name | Format-Table -Auto
#几种方法导出到文件
$resultTable.GetEnumerator() | sort Name | Format-Table -Auto | Out-File “D:\360Downloads\PerfmonCounter.txt”
$resultTable.GetEnumerator() | sort Name | Export-Csv -Path “D:\360Downloads\PerfmonCounter.txt” -Encoding “unicode” -Force
$resultTable.GetEnumerator() | sort Name | Format-List | Export-Csv -Path “D:\360Downloads\PerfmonCounter.xlsx” -Encoding “unicode” -Force