Stephen Sugumar
Stephen Sugumar

Reputation: 545

Powershell Pipeline not being stored in variable

I need a Powershell script which reads a process on a remote computer and stores some information into a text file.

I have these queries located within a ForEach looping thtough an array of servers:

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"
}

Issue is, when the server = "DCOBB4" the $w3wpresult is not being stored within the $w3wpMemory object.

Located below is a sample of the output of the query:

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

Any help is greatly appreciated, Thanks!

Upvotes: 0

Views: 70

Answers (1)

user4003407
user4003407

Reputation: 22132

There is some odd behavior when turning [PSCustomObject] to [string]. In some cases (in particular, when [PSCustomObject] is a part of array) it results in empty string instead of desired value:

$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"

As workaround, you can turn [PSCustomObject] to [string] one by one, and then join results:

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

Upvotes: 1

Related Questions