3

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?

8
  • PowerShell is doing the best it can with that data. It does not know how to represent that object as a string in that "cell". You need to tell it what to do. Currently You do not show us what you expect for output. CSV are collections of string delimited data. If you want to retain that data structure you need to use JSON or XML ... etc.
    – Matt
    Commented Feb 24, 2017 at 17:25
  • Post has been updated to include a screenshot of what i see via out-gridview, this is what' i'm looking to get into the CSV
    – Kenny
    Commented Feb 24, 2017 at 18:09
  • This blog post by Boe Prox will help explain the issue. Note: the script center link at the bottom is broken the script can be found on his GitHub
    – BenH
    Commented Feb 24, 2017 at 18:16
  • You should use -Join or out-string with trim() Commented Feb 24, 2017 at 18:25
  • Thank you, i'll review the Boe Prox post again, i'd found it earlier, but didn't run through all his suggestions
    – Kenny
    Commented Feb 24, 2017 at 19:52

2 Answers 2

2

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
2
  • Thx, Mathais, i'll try that out
    – Kenny
    Commented Feb 26, 2017 at 23:18
  • This only works if the single property is desired. The OP appears to be asking for everything
    – Matt
    Commented Feb 28, 2017 at 22:19
1

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.

3
  • Thank you for this info. I tried out-string, but the results are just blank, odd.
    – Kenny
    Commented Feb 24, 2017 at 19:51
  • Are the results blank when you open Excel? The data is there but you might not see it in that collapsed cell. There is whitespace before and after the "table" that might be hiding you results. Does the text file contain the data?
    – Matt
    Commented Feb 24, 2017 at 21:17
  • Ah! Yes, it's there, just have to scroll down! Ok, getting somewhere with this now
    – Kenny
    Commented Feb 24, 2017 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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