2015-08-15 66 views
0

我需要一個Powershell腳本來讀取遠程計算機上的進程並將一些信息存儲到文本文件中。Powershell管道沒有存儲在變量中

我有這些查詢位於一個foreach循環thtough服務器陣列內:

if(($server.equals("DCOBB3")) -Or ($server.equals("DCOBB4"))) 
{ 
    write-host "inside if with " $server 
    $w3wpresult = (get-wmiobject Win32_Process -filter "name like 'w3wp%'" -computername $server | select name, @{l= "Private Memory (GB)"; e={$_.privatepagecount/1gb}}) 
    write-host $w3wpresult 
    $vmresult = (get-wmiobject Win32_Process -filter "name like 'w3wp%'" -computername $server | select name, @{l= "Virtual Memory (GB)"; e={$_.virtualsize/1gb}}) 
    $vmMemory += $server + " @ " + $time + ": " + $vmresult + "`r`n" 
    $w3wpMemory += $server + " @ " + $time + ":" + $w3wpresult + "`r`n" 
} 

問題是,當服務器=「DCOBB4」的$w3wpresult沒有被存儲在所述$w3wpMemory對象內。

位於下面是查詢的輸出的示例:

inside if with DCOBB3 
@{name=w3wp.exe; Private Memory (GB)=0.00959014892578125} 
DCOBB4 

inside if with DCOBB4 
@{name=w3wp.exe; Private Memory (GB)=0.0100898742675781} @{name=w3wp.exe;  
Private Memory (GB)=0.00747299194335938} @{name=w3wp.exe; Private Memory (GB)=0.0089874267578125} 

---------- W3WP Memory Consumption ---------- 
AIDE500-V14 @ 0948:@{name=w3wp.exe; Private Memory (GB)=0.689697265625} 
AIDE502-V12 @ 0948:@{name=w3wp.exe; Private Memory (GB)=0.0477447509765625} 
AIWE80-005 @ 0948:@{name=w3wp.exe; Private Memory (GB)=0.932292938232422} 
AIDE11-004 @ 0948: 
AIDE11-005 @ 0948: 
DCOBB3 @ 0948:@{name=w3wp.exe; Private Memory (GB)=0.00959014892578125} 
DCOBB4 @ 0948 

任何幫助是極大的讚賞, 謝謝!

+1

'+ $ w3wpresult +' - >'+(($ w3wpresult |%{ 「$ _」}) - 加入')+' – PetSerAl

+0

由於這是答案, 希望把它作爲所以我可以標記它? –

回答

1

[PSCustomObject]轉換爲[string]時有一些奇怪的現象。在某些情況下(特別地,當[PSCustomObject]是陣列的一部分)它導致空字符串,而不是所需的值:

$a=[PSCustomObject]@{a=1} 
$a.ToString()   # return "" 
$a.PSObject.ToString() # return "@{a=1}" 
[string]$a    # return "@{a=1}" 
"$a"     # return "@{a=1}" 
"$(2,$a,3)"   # return "2 3" 
$a-join','    # return "@{a=1}" 
(2,$a,3)-join','  # return "2,,3" 

作爲變通方法,可以一個接一個地打開[PSCustomObject][string],然後再加入的結果:

($w3wpresult|%{"$_"})-join','