Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have an awesome script that gives vital data for running worker processes. What I'm attempting to do is to get these three values into a table so that they can be displayed all pretty-like. I previously used a .net extension to accomplish this but I've add problems with compatibility so I'd like to use strictly Powershell here. Here is my script:

$myprocess = "w3w*"
$filter = "*\process(" + $myprocess + ")*"
$pids = get-counter -listset process | get-counter -maxsamples 1 | select -expandproperty countersamples | where {$_.path -like "*\id process" -and     $_.path -like $filter} | select cookedvalue | ForEach {$_.cookedvalue}
$wsp = get-counter -listset process | get-counter -maxsamples 1 | select -expandproperty countersamples | where {$_.path -like "*\working set" -and $_.path -like $filter} | select cookedvalue | ForEach {$_.cookedvalue}
$w3wpn = $pids | foreach {
$filter = "processid='" + $_ + "'"
$wmip = gwmi Win32_Process -filter $filter
if($wmip.CommandLine -match "-ap `"(.+)`"") {
$appName = $matches[1] -replace '([^^"].)".*', '$1'
Write-Output $appName
}}

'WP,IP,Memory',$w3wpn,$pids,$wsp | ConvertFrom-CSV | Format-Table -Auto

and here is the output I'm looking for:

WP                  Process ID Mem (KB)     
--                  ---------- --------     
MyAppPool                 4816 76,435,456   
AnotherAppPool           16924 205,369,344  
DefaultAppPool           34508 233,574,400  
share|improve this question

1 Answer 1

Figured it out!

The key here was piping it out to an array successfully. I've had a lot of drawbacks here but you've got to work thru them.

$pids = get-counter -listset process | get-counter -maxsamples 1 | select -expandproperty countersamples | where {$_.path -like "*\id process" -and       $_.path -like $filter} | select cookedvalue | ForEach {$_.cookedvalue}
function GetMemoryUsage  { ...code here... }
function GetAppID { ...code here.... }
$myresults = @()
$x = foreach ($ids in $pids) {
 $thisapp = GetAppID $ids
 $thismem = GetMemoryUsage $ids
 $robj = New-Object System.Object
 $robj | Add-Member -type NoteProperty -name WP -value $thisapp
 $robj | Add-Member -type NoteProperty -name "Process ID" -value $ids
 $robj | Add-Member -type NoteProperty -name Memory -value $thismem

 $myresults += $robj
}
$myresults | ft -auto

The great thing abou this method is that you can pipe in an infinite number of variables and as long as you can relate these to the array then you're in business.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.