3

I need help in PowerShell to combine two outputs or two PSCustomObjects into One. For example,

$services = Get-Service | Select Name, Starttype,Status
$processes = Get-Process | Select ID

I need the output with the table headers Name, Starttype, Status, ID

I have already tried creating CSV and joining them but the problem is Process ID starts when the entire output ends for the services. I need them to a parallel.

Second I have tried to create PSCustomObjects but no luck.

Please help me with the PowerShell code.

Actual code that I'm trying to achieve.

**$exclusionItems = @()
$OasHighItems = @()   
foreach($item in $items){
    $exclusionItems  += [PSCustomObject]@{
        EXCLUSION_BY_NAME_OR_LOCATION = $item.EXCLUSION_BY_NAME_OR_LOCATION
        EXCLUSION_EXCLUDE_SUBFOLDERS = $item.EXCLUSION_EXCLUDE_SUBFOLDERS
        EXCLUSION_ON_READ= $item.EXCLUSION_ON_READ
    }
    
}
foreach($oas in $oashigh){
    $oashighItems += [PSCustomObject]@{
        OAS_PROCESSES_LIST = $oas
    }
}
$Array = @()
$Array = $exclusionItems,$oashighItems
$Array | Update-FirstObjectProperties | Export-Excel $ExcelParams -TableName Table -Show**
Chirag
  • 33
  • 4
  • 3
    how are you associating the items in `Get-Service` with the items in `Get-Process`? – Lee_Dailey Dec 17 '20 at 13:00
  • It is just an example, I have few XML files and each file output has different PSCustomObject outputs. For example, the First output has three headings, and the second has one. I need to combine them into one. – Chirag Dec 17 '20 at 13:23
  • Does this answer your question? [Combining Multiple CSV Files](https://stackoverflow.com/questions/17752072/combining-multiple-csv-files) or [In Powershell, what's the best way to join two tables into one?](https://stackoverflow.com/a/45483110/1701026) or [Powershell. Combine properties of objects in array](https://stackoverflow.com/questions/64805051/powershell-combine-properties-of-objects-in-array) – iRon Dec 17 '20 at 13:44
  • 1
    @Chirag, if the linked posts do not solve your problem, please update your question with representative sample input and expected output. – mklement0 Dec 17 '20 at 13:55
  • @Chirag - then you 1st need to decide how to associate any given item in the 1st collection with any given item in the 2nd collection. you also need to provide a realistic set of input data AND how you want the output data to look. – Lee_Dailey Dec 17 '20 at 13:55

1 Answers1

0

I'm assuming you want to join the two objects by their names, i.e. match the Process-Name with the Service-Name. For this you can loop over all processes & services, keep only those where service-name equals process-name, and use a calculated property to merge the result into one object:

$services = Get-Service;
Get-Process | ForEach-Object {$p = $_;  $services |
                Where-Object{$p.ProcessName -eq $_.Name} |
                    Select-Object Name,StartType,Status,@{n='ID';e={$p.ID}}} 

The output on my machine is:

Name                  StartType  Status    ID
----                  ---------  ------    --
CcmExec               Automatic Running 14856
CmRcService           Automatic Running  5748
FusionInventory-Agent Automatic Running  5996
IBMPMSVC              Automatic Running  3540
IntelAudioService     Automatic Running  6104
... and so on ...
davidhigh
  • 14,652
  • 2
  • 44
  • 75