Forum Discussion
Farhad_Ostovar
Jun 22, 2021Copper Contributor
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...
Farhad_Ostovar2265
Jun 22, 2021Copper Contributor
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
#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
yuzoyox
Jun 22, 2021Iron Contributor
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
#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
- Farhad_Ostovar2265Jun 22, 2021Copper ContributorTried that but unfortunately it generated another blank CSV.
- yuzoyoxJun 22, 2021Iron ContributorGot it, change write-host to write-output
Write-Output "There are $countExpiring certs." -ForegroundColor Green >> c:\temp\report.csv- Farhad_Ostovar2265Jun 23, 2021Copper ContributorDone but unfortunately it generated an excel sheet with the below info.
There are 27 certs.
#NAME?
Green