azurepowershellazure-powershellpowershell-remotingazure-connect

How to Combining PowerShell Output from multiple server to Single csv


I need to pull the report from below code, now I'm running this in every server separately.

Is it possible to combine the the all output from different server and get it in one csv file or get it to my email directedly.

where to define the servers name ? how to display the server name in every output ? how to combine and get in single output ?

Get-ADSyncToolsRunHistory | Where {$_.RunProfileName -match "Delta Import"} | Select-Object -Index 0, 1 | Format-Table StartDate, EndDate, ConnectorName, RunProfileName, Result
Get-ADSyncToolsRunHistory | Where {$_.RunProfileName -match "Delta Synchronization"} | Select-Object -Index 0, 1 | Format-Table StartDate, EndDate, ConnectorName, RunProfileName, Result
Get-ADSyncToolsRunStepHistory | Where {$_.RunProfileName -match "Full Import"} | Select-Object -Index 0, 1 | Format-Table StartDate, EndDate, ConnectorName, RunProfileName, StepResult
Get-ADSyncToolsRunStepHistory | Where {$_.RunProfileName -match "Full Synchronization"} | Select-Object -Index 0, 1 | Format-Table StartDate, EndDate, ConnectorName, RunProfileName, StepResult



Solution

  • you could do this from local, to start with: in servers.txt file - add your servers hostname line by line like

    server1
    server2
    
    $servers = Get-Content "c:\temp\servers.txt"
    
    $Report = foreach ($server in $servers){
        Invoke-Command -ComputerName $server -ScriptBlock{
            Get-ADSyncToolsRunHistory | Where {$_.RunProfileName -match "Delta Import"} | Select-Object @{N='ServerName';E={"$env:COMPUTERNAME"}},StartDate, EndDate, ConnectorName, RunProfileName, Result
        }
    }
    
    $Report|Export-Csv "c:\temp\DeltaImport.csv" -NoTypeInformation
    

    Before Using Invoke-Command, I suggest you to go through this link once: https://4sysops.com/archives/use-powershell-invoke-command-to-run-scripts-on-remote-computers/