Home > Software engineering >  How to export all data in single csv file in Powershell
How to export all data in single csv file in Powershell

Time:11-25

how to combine all result in single cvs file in table form.

Get-ADSyncToolsRunstepHistory | Where {$_.RunProfileName -match "Delta Import"} |Select-Object -Index 0, 1|Select-Object @{N='ServerName';E={"$env:COMPUTERNAME"}} ,StartDate, EndDate, ConnectorName, RunProfileName, StepResult
Get-ADSyncToolsRunstepHistory | Where {$_.RunProfileName -match "Delta Synchronization"} |Select-Object -Index 0, 1|Select-Object @{N='ServerName';E={"$env:COMPUTERNAME"}} ,StartDate, EndDate, ConnectorName, RunProfileName, StepResult
Get-ADSyncToolsRunstepHistory | Where {$_.RunProfileName -match "Full Import"} |Select-Object -Index 0, 1|Select-Object @{N='ServerName';E={"$env:COMPUTERNAME"}} ,StartDate, EndDate, ConnectorName, RunProfileName, StepResult
Get-ADSyncToolsRunstepHistory | Where {$_.RunProfileName -match "Full Synchronization"} |Select-Object -Index 0, 1|Select-Object @{N='ServerName';E={"$env:COMPUTERNAME"}} ,StartDate, EndDate, ConnectorName, RunProfileName, StepResult

CodePudding user response:

If I understand correctly you want to export all results into a single CSV, in that case, it could be done this way:

$props = @(
    @{
        N='ServerName'
        E={ $env:COMPUTERNAME }
    }
    'StartDate'
    'EndDate'
    'ConnectorName'
    'RunProfileName'
    'StepResult'
)

$profiles = @(
    'Delta Import'
    'Delta Synchronization'
    'Full Import'
    'Full Synchronization'
) -join '|'

Get-ADSyncToolsRunstepHistory | Group-Object RunProfileName |
    Where-Object Name -Match $profiles | ForEach-Object {
        # select the first 2 objects for each group of objects
        # matching the `$profiles`
        $_.Group | Select-Object -First 2
    } | Select-Object $props | Export-Csv path\to\export.csv -NoTypeInformation

This uses Group-Object and Where-Object to filter the first 2 objects of each profile, this way there is a single call to Get-ADSyncToolsRunstepHistory.

  • Related