$Path = 'C:\';
$Folder = 'PerfLogs';
$FullPath = ($Path + $Folder + '\');
$CounterFile = 'Counters.txt';
$LoopSeconds = 5;
$LogKeepDays = 30;
$Now = (Get-Date);
if ((Test-Path $FullPath) -eq $False) {
write-host ('Path ' + $FullPath + ' not found.');
New-Item -Path $Path -Name $Folder;
exit;
};
Set-location $FullPath
$Files = Get-ChildItem $FullPath | where {$_.Extension -eq '.log'} | Select Name,FullName,LastAccessTime
ForEach ($File in $Files) {
if ($File.LastAccessTime -gt ($File.LastAccessTime.AddDays($LogKeepDays))) {
Get-ChildItem -Path $FullPath | where {$_.Name -eq $File.Name} | Remove-Item -force -Recurse -ErrorAction SilentlyContinue;
};
};
New-Item -Path $FullPath -Name $CounterFile -ItemType "file" -Value "\Processor(_Total)\% Processor Time`r`n\Memory\% Committed Bytes In Use`r`n\PhysicalDisk(_Total)\% Disk Time" -Force;
C:\windows\system32\typeperf.exe -cf $CounterFile -si $LoopSeconds -f TSV -o ('PerfMon_' + $Now.Year + $Now.Month.ToString("D2") + $Now.Day.ToString("D2") + '_' + $Now.Hour.ToString("D2") + $Now.Minute.ToString("D2") + $Now.Second.ToString("D2") + '.log')
Collector 查詢: typeperf -qx
Reference: 黑暗執行續 - Windows CPU/RAM/Disk 效能監視 CLI 工具 - typeperf
沒有留言:
張貼留言