Skip to content

Instantly share code, notes, and snippets.

@matsest
Created January 7, 2022 14:24
Show Gist options
  • Save matsest/d823b77186e7e77cb671a691d7fce21b to your computer and use it in GitHub Desktop.
Save matsest/d823b77186e7e77cb671a691d7fce21b to your computer and use it in GitHub Desktop.
az-service-principal
#requires -modules @{ ModuleName="Az.Resources"; ModuleVersion="5.0.0" }
[CmdletBinding()]
param(
[Parameter(HelpMessage = 'Will output credentials if withing this number of days, use 0 to report only expired and valid as of today')]
$ExpiresInDays = 90
)
Write-Host 'Gathering necessary information...'
$applications = Get-AzADApplication
$appWithCredentials = @()
$appWithCredentials += $applications | Sort-Object -Property DisplayName | ForEach-Object {
[Microsoft.Azure.PowerShell.Cmdlets.Resources.MSGraph.Models.ApiV10.IMicrosoftGraphApplication]$application = $_
Write-Verbose ('Fetching information for application {0}' -f $application.DisplayName)
[Microsoft.Azure.PowerShell.Cmdlets.Resources.MSGraph.Models.ApiV10.IMicrosoftGraphPasswordCredential]$cred = $application | Get-AzADAppCredential -ErrorAction SilentlyContinue
$cred | Select-Object -Property `
@{
Name = 'DisplayName';
Expression = { $application.DisplayName }
},
@{
Name = 'ObjectId';
Expression = { $application.Id }
},
@{
Name = 'AppId';
Expression = { $application.AppId }
},
@{
Name = 'KeyId';
Expression = { $cred.KeyId }
},
@{
Name = 'StartDate';
Expression = { $_.StartDateTime -as [datetime] }
},
@{
Name = 'EndDate';
Expression = { $_.EndDateTime -as [datetime] }
}
}
Write-Host "Validating expiration data..."
$today = (Get-Date).ToUniversalTime()
$limitDate = $today.AddDays($ExpiresInDays)
$appWithCredentials | Sort-Object EndDate | ForEach-Object {
if ($_.EndDate -lt $today) {
$_ | Add-Member -MemberType NoteProperty -Name 'Status' -Value 'Expired'
}
elseif ($_.EndDate -le $limitDate) {
$_ | Add-Member -MemberType NoteProperty -Name 'Status' -Value 'ExpiringSoon'
}
else {
$_ | Add-Member -MemberType NoteProperty -Name 'Status' -Value 'Valid'
}
}
$appWithCredentials
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment