U bent hier

PowerShell script to log memory usage per process in .csv

This script is written to log a process list into a .csv file so it can be imported for analysis in Excel. Subsequent calls to the process result in data to be appended to the .csv file. Date and time are part of the columns exported, so the imported .csv in Excel can be used for a clear PivotTable or PivotChart to see memory usage evolution in time on the monitored server.

$Processes = get-process
$gDate = Get-Date -format "yyyyMMdd"
$gTime = Get-Date -format "HH:mm"
$gFileName = "c:\scripts\logs\RegisterProcessMemoryUsage$(get-date -f yyyyMMdd).csv"
foreach($Process in $Processes)
{
    $Obj = New-Object psobject
    $Obj | Add-Member -MemberType NoteProperty -Name Date -Value $gDate
    $Obj | Add-Member -MemberType NoteProperty -Name Time -Value $gTime
    $Obj | Add-Member -MemberType NoteProperty -Name ID -Value $Process.Id
    $Obj | Add-Member -MemberType NoteProperty -Name Name -Value $Process.Name
    $Obj | Add-Member -MemberType NoteProperty -Name PagedMem -Value $Process.PagedMemorySize64
    $Obj | Add-Member -MemberType NoteProperty -Name PagedSysMem -Value $Process.PagedSystemMemorySize64
    $Obj | Add-Member -MemberType NoteProperty -Name nonPagedSystemMem -Value $Process.NonpagedSystemMemorySize64
    $Obj | Add-Member -MemberType NoteProperty -Name Responding -Value $Process.Responding
    $Obj | Export-Csv -Path $gFileName -Delimiter "," -Append -NoTypeInformation
}

#This removes double quotes from the numeric columns (content of the regexp depends on the Add-Members above)
(Get-Content $gFileName) -replace '^"(.*?)",(.*?),"(.*?)",(.*?),"(.*?)","(.*?)","(.*?)",(.*?)$', '$1,$2,$3,$4,$5,$6,$7,$8' | Set-Content $gFileName