Can't align result into table. Two commands

Hi Everyone,

I would like to create a report to email the replication status. But I can’t align the results on the table, the 2nd command creates new columns.

The command both have PSComputerName, so I want to align them if they have the same PSComputerName. I’m still new to PS scripting and I wanna know if there’s a better way to do this like new custom object?

Invoke-Command -ComputerName ("Server1","Server2","Server3") -ScriptBlock { (Get-SRPartnership | select SourceComputerName, SourceRGName, DestinationComputerName, DestinationRGName)} | Sort-Object PSComputerName | Export-CSV -Path "C:\SRPReport.csv" -NoTypeInformation
Invoke-Command -ComputerName ("Server1","Server2","Server3") -ScriptBlock { (Get-SRGroup | select IsSuspended, ReplicationMode, ReplicationStatus)} | Sort-Object PSComputerName | Export-CSV -Path "C:\SRGReport.csv" -NoTypeInformation

Below is a snip of ps1 for emailing the results of the two generated csv.

$csv = Import-Csv $output | Select "SourceComputerName", "SourceRGName", "DestinationComputerName", "DestinationRGName", "IsSuspended", "ReplicationMode", "ReplicationStatus" | ConvertTo-Html -head $a -Body "<H2>Storage Replica Status of File Servers</H2>"

Thanks in Advance.

Regards,
Kent

Well you could predefined the script block and pass everything in one block and then sort all of the results.

My interpretation is you want results from both commands combined and grouped by hostname.

If you want to combine two different objects, then that is exactly what you need to do, create a new custom object. You take the properties you desire from each object into your final combined object. You can simplify your code greatly.

$scriptblock = {
    $partnership = Get-SRPartnership
    $group = Get-SRGroup

    [PSCustomObject]@{
        SourceComputerName      = $partnership.SourceComputerName
        SourceRGName            = $partnership.SourceRGName
        DestinationComputerName = $partnership.DestinationComputerName
        DestinationRGName       = $partnership.DestinationRGName
        IsSuspended             = $group.IsSuspended
        ReplicationMode         = $group.ReplicationMode
        ReplicationStatus       = $group.ReplicationStatus
    }
}

$output = Invoke-Command -ComputerName "Server1","Server2","Server3" -ScriptBlock $scriptblock

$output | Sort-Object -Property PSComputerName |
    Export-Csv -Path "C:\CombinedReport.csv" -NoTypeInformation
2 Likes

Hi kryzdoug,

Thank you so much. That works like a charm.
Will take note of creating a custom object on my future scripts.

Thank you again.

Best Regards,
Kent