Exporting Powershell output

Occasional Visitor

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?

 

  1. #Change this to the number of days out you want to look
  2. $daysOut = 90
  3. #Main Script#
  4. $doneID = ""
  5. $countExpiring = 0
  6. $allSAMLApps = Get-AzureADServicePrincipal -All $true
  7. Write-Host "Looking for certs that expire by "((Get-Date).AddDays($daysOut)) -ForegroundColor Green
  8. foreach ($singleApp in $allSAMLApps) {
  9. foreach ($KeyCredential in $singleApp.KeyCredentials) {
  10. if ( $KeyCredential.EndDate -lt (Get-Date).AddDays($daysOut) ) {
  11. if (($singleApp.ObjectId) -ne $doneID) {
  12. Write-Host " Name: " ($singleApp.DisplayName) " - Experation: " $KeyCredential.EndDate
  13. $doneID = ($singleApp.ObjectId)
  14. $countExpiring = $countExpiring + 1
  15. }
  16. }
  17. }
  18. }
  19. Write-Host "There are $countExpiring certs." -ForegroundColor Green
13 Replies
Hi Farhad
If the last line is printing correctly but not generating the file, try add pipeline and put it in .csv

Write-Host "There are $countExpiring certs." -ForegroundColor Green | Export-CSV c:\folderpath\output.csv -NoTypeInformation
Thanks for the response. I tried running that but the CSV generated is blank. I used the below script.


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


#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 | Export-CSV c:\temp\report.csv -NoTypeInformation
lets try with cmd command:

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


#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 >> c:\temp\report.csv
Tried that but unfortunately it generated another blank CSV.
Got it, change write-host to write-output

Write-Output "There are $countExpiring certs." -ForegroundColor Green >> c:\temp\report.csv
Done but unfortunately it generated an excel sheet with the below info.

There are 27 certs.
#NAME?
Green
I change all the write to output and send information to text file to see what iss getting for your script

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


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

$allSAMLApps = Get-AzureADServicePrincipal -All $true

Write-Output "Looking for certs that expire by "((Get-Date).AddDays($daysOut)) -ForegroundColor Green >> c:\temp\report.csv
foreach ($singleApp in $allSAMLApps) {

foreach ($KeyCredential in $singleApp.KeyCredentials) {

if ( $KeyCredential.EndDate -lt (Get-Date).AddDays($daysOut) ) {
if (($singleApp.ObjectId) -ne $doneID) {
Write-Output " Name: " ($singleApp.DisplayName) " - Experation: " $KeyCredential.EndDate >> c:\temp\report.csv
$doneID = ($singleApp.ObjectId)
$countExpiring = $countExpiring + 1
}
}

}

}
Write-Output "There are $countExpiring certs." -ForegroundColor Green >> c:\temp\report.csv
Nice that worked. Thanks so much! Only issue is that it exported it all unorganized. I am assuming that is how the script is meant to generate info?

Looking for certs that expire by

Friday, March 2, 2035 9:24:27 AM
#NAME?
Green


Name:
Salesforce_AFP_standard
- Experation:

Monday, July 25, 2022 5:46:48 PM


Name:
Quorum
- Experation:

Saturday, April 6, 2024 2:40:10 PM


Name:
Zscaler Admin SSO
Nice it worked. Now you have to work in how to organize it.

Thank you
Hi Farhad, could you please mark the last code I sent as accpeted solution?
thank you very much
regards
Sure, but not seeing that option anywhere.

@Farhad_Ostovar2265 

There is no option like this?

yuzoyox_0-1624554436541.png

 

@yuzoyox nope. 

 

 

Farhad_Ostovar2265_0-1624557090917.png