Tuesday, December 19, 2006

Memory consumption

Here are two small script used for displaying the name of currently running processes, as well as the amount of memory currently allocated (through VMM) to Virtual Machines (either Microsoft Virtual PC or Virtual Server):

function GetMem {
    $processes = Get-WmiObject win32_process | Sort-Object -property WorkingSetSize -descending
    $processes | format-table `
         ProcessName, `
         Handle, `
         WorkingSetSize, `
         @{label="in MB"; Expression={([Math]::round($_.WorkingSetSize / 1mb,1))}} `
         -autosize
    $TotalMem= 0
    foreach ($process in $processes) {
         $TotalMem = $TotalMem + $process.WorkingSetSize
    }
    $TotalMem_in_MB = [math]::round($TotalMem/1mb,1)
    Write-Output "`tTotal mem used: `t $TotalMem ($TotalMem_in_MB MB)`n"
}

$a = GetMem; $a

function GetVMmem {
    $virtualmachines = Get-WmiObject virtualmachine -namespace "root\vm\virtualserver"
    $virtualmachines | format-table `
         @{label="ProcessName"; Expression={("VirtualMachine " + $_.Name)}}, `
         PhysicalMemoryAllocated, `
         @{label="in MB"; Expression={([Math]::round($_.PhysicalMemoryAllocated/1mb,1))}} `
         -autosize
    $TotalMem = 0
    foreach ($virtualmachine in $virtualmachines) {
         $TotalMem = $TotalMem + $virtualmachine.PhysicalMemoryAllocated
    }
    $TotalMem_in_MB = [math]::round($TotalMem/1mb,1)
    Write-Output "`tTotal mem used: `t $TotalMem ($TotalMem_in_MB MB)`n"
}

$b = GetVMmem; $b

1 comment:

Spundae said...

This is a beautiful script. I absolutely love it. I have placed this in my script to see the actual memory usage of remote systems. Thank you very much for posting this