2012-06-20 55 views
0

所以我幾乎得到了我最初的問題,但我在這裏遇到了一堵磚牆:我有一組3個變量(最終會變得更多),需要被格式化成表格。我創建了每個變量的功能,我可以使用下面的代碼成功地結合在一起的兩個變量:Powershell使用多種功能創建新對象

$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.... } 
$combined = $pids | %{ $wapp = GetAppID $_ 
      $obj = new-object psobject 
      $obj | add-member -name WP -type noteproperty -value $wapp 
      $obj | add-member -name "Process ID" -type noteproperty -value $_ 
      $obj 
     } 
Write-Output $combined 

我的問題是:如何添加GetMemoryUsage這裏對象?我試過使用一個函數,但它不太好。請注意,我不想在這裏使用哈希表,因爲我相信他們只能在v2中工作,而且我還有機器運行v1。謝謝!

回答

0

如果進程ID和內存消耗是你以後有什麼那麼這應該爲你工作:

Get-Process | Select Name,ID,WorkingSet 
+0

我需要得到工作集 - 私人。不過謝謝! –

+0

您是否在Get-Process的輸出中檢查了PrivateMemorySize屬性? –

0

想通了!

這裏的關鍵是成功地將其輸出到一個數組。我在這裏有很多缺點,但你必須通過他們。

$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 Memory -value $thismem 
    $robj | Add-Member -type NoteProperty -name Process -value $ids 
    $myresults += $robj 
} 
$myresults | ft -auto 

安博這種方法的好處是,你可以管中的變量無限多隻要你可以涉及到這些陣列,那麼你在企業裏。