question

FarhadOstovar-6368 avatar image
0 Votes"
FarhadOstovar-6368 asked MotoX80 answered

Exporting Powershell output

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MotoX80 avatar image
0 Votes"
MotoX80 answered

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                   
  }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.