locked
How to display two Objects values in single command RRS feed

  • Question

  • Hi,

    I am extracting list of azure service principals with Start and End date details in a single file. I am executing below script but I could not able to select three values in single command and export into a single csv file.

    Below is script:

    $ServicePrincipalIds = Get-AzADServicePrincipal | Where DisplayName -eq 'adminpoc-test-adls'
    
    $ServicePrincipalName = $ServicePrincipalIds.DisplayName
    
    foreach($ServicePrincipalId in $ServicePrincipalIds)
    {
    
    $ServicePrincipalInfo = Get-AzADSpCredential -ObjectId $ServicePrincipalId.Id
    
    $finaldate = ($ServicePrincipalInfo | Sort-Object {$_."EndDate" -as [DateTime] } -Descending | select -First 1) 
    
    $string = [pscustomobject]@{
        "ServicePrincipalName" = $ServicePrincipalName
        }
    
    $finaldate | select StartDate,EndDate
    $string | select ServicePrincipalName
    
    }


    Below is Output:

    StartDate           EndDate            
    ---------           -------            
    19-05-2020 06:46:10 19-05-2021 06:46:10

    Can you please help me how to select three values and export into csv file with proper column names and values.

    Thanks,

    Brahma

    Sunday, July 19, 2020 10:25 AM

Answers

  • Just put them all into the PSCustomObject and have the script/function return that in the pipeline.

    Remove the last two statements from your script/function (i.e., the ones that pipe $finaldate and $string into select cmdlets).

    [pscustomobject]@{
            ServicePrincipalName = $ServicePrincipalName
            StartDate = $finaldate.StartDate
            EndDate = $finaldate.EndDate
            }
    If you want those properties in a CSV, pipe the output for the script/function into an Export-CSV cmdlet.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Sunday, July 19, 2020 2:32 PM