Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Array to export-csv shows System.Object[]

Tags:

powershell

Having a simple issue that's only affecting export-csv output, out-gridview and results to the console are fine. Looking to capture the top 5 processes by "handles" on a set of servers.

Code is as follows:

$Servers = "Server1", "Server2", "Server3"
$OutArray = @()
ForEach ($Item in $Servers)

$Top5 = Get-Process -Computer $Item | Sort Handles -descending |Select -First 5

     $OutArray += New-Object PSObject -property @ {
     Server = $Item
     Top5 = $Top5

    } #OutArray

} #ForEach

$OutArray | Export-csv Test.csv

The results of which come out looking fine via console as follows

 Server   Top5                                                                                                                                                                                                         
 ------   ----                                                                                                                                                                                                         
 SERVER1 {@{ProcessName=svchost.exe; PercentCpuLoad=13.79}, @{ProcessName=services.exe; PercentCpuLoad=11.4}, @{ProcessName=WmiPrvSE.exe; PercentCpuLoad=10.03}, @{ProcessName=irfilcol.exe; PercentCpuLoad=9.79}...} 

...However, in the csv they show as follows:

Server  Top5
Server1 System.Object[]
Server2 System.Object[]
Server3 System.Object[]

I'm thinking it's because the $Top5 variable is an variable with multiple properties (5 each) for one server. How would do I correct the code so that export-csv shows the actual values?

any help appreciated!

I would like the csv results to look like the following that's shown in GRIDVIEW

enter image description here

Using the suggestion from BenH to review the post from Powershell legend Boe Prox, I now have the following working:

 $Top5 = Get-Process -Computer $Item | Sort Handles -descending |Select -expand Handles | |Select -First 5 
 $new = [pscustomobject]@{ Top5 = (@($Top5) -join ',')
 }

Just about got this working now:

i'd like to add more piece of formatting, where the Top5Processes have the actual CPU % used in (brackets) right now, I've got the following for output

 Top2Proc                            Top2CPU                  
services.exe,BESClient.exe           32.76,16.6

However, it would be nicer output-wise, if i could combine the above two values into one, so it looks like this:

 Top2Proc
 Services(32.76), BesClient.exe(16.6)

Any idea how that would be done?

like image 901
Kenny Avatar asked Jul 22 '26 03:07

Kenny


2 Answers

Use Select-Object to turn your process objects into strings before piping them to Export-Csv:

$OutArray |Select-Object Server,@{Expression={$_.Top5.Name -join ';'}} |Export-Csv test.csv
like image 169
Mathias R. Jessen Avatar answered Jul 23 '26 15:07

Mathias R. Jessen


If you want that table to appear in your csv file then you would need to format the string Top5 property as such. Using Out-String will do just that

Sends objects to the host as a series of strings.

So a simple change should get you what you want.

$Top5 = Get-Process -Computer $Item |
    Sort Handles -descending |
    Select -First 5 |
    Out-String

It will look a little ugly when not displayed with a mono-space font much like you see in Out-GridView. Also consider using .Trim() to remove the leading and trailing whitespace on your $top5.

There are other ways to tackle this. You could use the above in conjunction with Format-Table / Format-List depending what you want. In general if you want the output to be saved as it is displayed in host Out-String is something to test with.


I would have tried to add one row for each process with a the first column being the computer name. That way you would have better structured output that can be sorted or queried as needed.

ComputerName ProcessName Handles
------------ ----------- -------
Computer1    avp           54639
Computer1    OUTLOOK        7708
Computer1    RDTabs         6108
Computer1    svchost        3160
Computer1    chrome         2530

Keep in mind that you can use other methods to export this data while keeping the objects entact. Really depends the data recipeint but remeber there are other cmdlets like Export-CLIMXL and ConvertTo-JSON | Set-Content.

like image 25
Matt Avatar answered Jul 23 '26 16:07

Matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!