Read Process Name from csv and Get more Properties then Output csv

Hi,

I am trying to do something simple. I need to read Process Names such as explorer, services, winrm then want to pipe get-process and get the CPU, Description then export this to CSV.

$Processes = Import-Csv "importprocess.csv"

$Processes | ForEach-Object | Get-Process.CPU

get-process $Processes.Name | ConvertTo-Csv -NoTypeInformation | Out-File "c:\users\user\documents\processesoutput.csv"

 

I also want to get-process explorer then pipe to get-service specific properties from the get-service cmdlet and export these as a csv. So i have data from multiple cmdlets in a single csv.

 

Not terribly elegant but…

$Results = @()

$Processes = Import-Csv "importprocess.csv"

ForEach($Process in $Processes) {

$Results+=(Get-Process | ?{$_.Name -eq $Process }) | Select Name, CPU

}

$Results | Export-Csv -NoTypeInformation "c:\temp\processesoutput.csv"

The way you have this written, it will never work, even for the first two lines. You cannot use a cmdlet this way.

Get-Process.CPU

The is also no such property from the Get-Process cmdlet called CPU, the physical process does. You have to get the process object members to get that

So, this should get you on the path to what you are after.

# Get process list
$Processes = 'notepad','explorer'

# Pipe the process names from the list to Get-PRocess
# Get all object memberships for the target process
$Processes | 
% { 
    "`nShowing all object members for $_`n"
    Get-Process -Name $_ | Get-Member 
}

SHowing all object members for notepad



   TypeName: System.Diagnostics.Process

Name                       MemberType     Definition                                                                                                                                                 
----                       ----------     ----------                                                                                                                                                 
Handles                    AliasProperty  Handles = Handlecount                                                                                                                                      
Name                       AliasProperty  Name = ProcessName                                                                                                                                         
NPM                        AliasProperty  NPM = NonpagedSystemMemorySize64                                                                                                                           
...

SHowing all object members for explorer

Handles                    AliasProperty  Handles = Handlecount
Name                       AliasProperty  Name = ProcessName
NPM                        AliasProperty  NPM = NonpagedSystemMemorySize64
PM                         AliasProperty  PM = PagedMemorySize64                                                                                                                               
...                                                                    



# Get only selected members - screen output
$Processes | 
% {
    Get-Process -Name $_ | 
    Select-Object -Property Description,CPU
}

Description            CPU
-----------            ---
Notepad            0.21875
Windows Explorer 50.734375



# Get only selected members - file output
$Processes | 
% {
    Get-Process -Name $_ | 
    Select-Object -Property Description,CPU | 
    Out-File -FilePath 'D:\Temp\processesoutput.csv' -Append
}

Get-Content -Path 'D:\Temp\processesoutput.csv'

Description     CPU
-----------     ---
Notepad     0.21875



Description            CPU
-----------            ---
Windows Explorer 50.765625

Thank you this code works but the csv has no content.
This is the content of my import csv not sure if there is an issue with the format?

ProcessName
explorer,services

That is excellent Postanote.

I just have a problem with the csv output not being separated by commas?

I need to view it in Excel with separate columns so i can filter, sort etc.

 

Yep, that’s because I was just dumping to a file.
Make this change and you should be good to go.

You added what your file looked like, that you did not do in you original post.
You have to turn that file into a list, since it’s not a properly formatted csv.

ProcessName
explorer,services

$Processes = (Get-Content -Path 'D:\Temp\ProcessNames.csv' | Select -Skip 2) -split ','

# Results

explorer
services
# Get process list
$Processes = 'notepad','explorer','BuildService'

# Get only selected members - file output
$ProcessReport = $Processes | 
% { 
    Get-Process -Name $_ | 
    Select-Object -Property Description,CPU 
}

$ProcessReport | 
Sort-Object -Property Description | 
Export-Csv -Path 'D:\Temp\processesoutput.csv' -NoTypeInformation
Import-Csv -Path 'D:\Temp\processesoutput.csv'

# Results

Description                CPU      
-----------                ---      
IncrediBuild Agent Service 57.828125
Notepad                    0.21875  
Windows Explorer           94.265625

Thanks for the response and the code. It has been very helpful.

I still got blank output in my csv. I had to change it to Select -skip 1 instead to make it work.

I need to update the above script so that if the import csv file has a process which does not exist then it will just write that process name to the export csv instead of an error or write a specific string like None to all the output properties.

So instead of skipping lines i need the output lines to be the same count as the imported ones so i can paste the output into another excel file alongside the import csv as columns.

So basically If Process Name exists then do the output Else just write the process name or write the word None.