Exporting Powershell output

Farhad Ostovar 1 Reputation point
2021-06-22T15:57:06.04+00:00

Hello -

I am having lots of trouble exporting out the output when running the below script. I have tried different export commands but none seem to be working. Can someone please let me know if they know a way to export the output to CSV?

#Change this to the number of days out you want to look
$daysOut = 90


#Main Script#
$doneID = ""
$countExpiring = 0

$allSAMLApps = Get-AzureADServicePrincipal -All $true

Write-Host "Looking for certs that expire by "((Get-Date).AddDays($daysOut)) -ForegroundColor Green
foreach ($singleApp in $allSAMLApps) {

    foreach ($KeyCredential in $singleApp.KeyCredentials) {

        if ( $KeyCredential.EndDate -lt (Get-Date).AddDays($daysOut) ) {
            if (($singleApp.ObjectId) -ne $doneID) {
                Write-Host " Name: " ($singleApp.DisplayName) " - Experation: " $KeyCredential.EndDate
                $doneID = ($singleApp.ObjectId)
                $countExpiring = $countExpiring + 1
            }
        }

    }

}

Write-Host "There are $countExpiring certs." -ForegroundColor Green
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,356 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 31,571 Reputation points
    2021-06-22T17:26:11.75+00:00

    Try this.

      #Change this to the number of days out you want to look
     $daysOut = 90
    
    
     #Main Script#
     $doneID = ""
     $countExpiring = 0
     $csv = @()                                                 # define empty array 
    
    
     $allSAMLApps = Get-AzureADServicePrincipal -All $true
    
     Write-Host "Looking for certs that expire by "((Get-Date).AddDays($daysOut)) -ForegroundColor Green
     foreach ($singleApp in $allSAMLApps) {
    
         foreach ($KeyCredential in $singleApp.KeyCredentials) {
    
             if ( $KeyCredential.EndDate -lt (Get-Date).AddDays($daysOut) ) {
                 if (($singleApp.ObjectId) -ne $doneID) {
                     Write-Host " Name: " ($singleApp.DisplayName) " - Experation: " $KeyCredential.EndDate
                     # Add the entry
                     $csv += [PSCustomObject]@{
                            Name       = $singleApp.DisplayName
                            Expiration = $KeyCredential.EndDate
                     }
                     $doneID = ($singleApp.ObjectId)
                     $countExpiring = $countExpiring + 1
                 }
             }
    
         }
    
     }
    
     Write-Host "There are $countExpiring certs." -ForegroundColor Green
     if ($countExpiring -gt 0 ) {
        $csv | Export-Csv -Path c:\temp\test.csv -NoTypeInformation                # generate our csv                   
     }
    
    0 comments No comments