Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How to Find Expiring Client Secrets Using Graph API and PowerShell

January 29, 2024
in Blog, Microsoft Graph, Powershell
0
ADVERTISEMENT

Table of Contents

Client secrert in Entra admin center

Microsoft removed the non-expiring option back in April 2021. That means that we have to renew client secrets all the time (before they expire). Below is a screenshot when creating a client secret for an app in the Entra admin center.

Vf2CGM8rnwM3eQb5KVi0ckyVYQ0sfe8YZ2FY1wfmIhAFRkBIdvDe6EOkgE1Y

When managing several tenants, it can be a hell of a job to monitor and maintain all the client secrets. Therefore, we’ve created a PowerShell script to query all the client secrets created within a single tenant.

bQfqmYn86n1x76BxwtdKJiTc4p4CYxRsXT16T3m859mxdcZ4cd6lwtTkC2fc

The script simply queries all the Entra ID Applications and their client secrets. If the client secret has an expiration date less than 30 or 90 days, it will report back to the console.

Graph Explorer

For those who don’t know about Graph Explorer should definitely have a look at it! With Graph Explorer you can query tenant data using Graph API. We were able to find the endDateTime value of a client secret. This is the data we’re looking for!

FMTMauPVTktR6EMBUCOdD81PcZPzJOACiUhltsj3Nm45zFmVzG1PDXV8WFo4

PowerShell

With the use of this PowerShell script, you can retrieve all applications in Entra ID and query all the client secrets attached to the applications. If they have an expiring client secret within 30 days, it will report back to the console which application and client secret should be rotated before it expires.

# Install required module if not already installed
Install-Module -Name Microsoft.Graph.Authentication -Force -AllowClobber
 
# Import required modules
Import-Module Microsoft.Graph.Authentication
 
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Application.Read.All"
 
# Retrieve all applications
$allApplications = @()
$pageSize = 100
$nextLink = $null
 
do {
    $applicationsPage = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/applications\$($nextLink -replace '\?', '&')"
    $allApplications += $applicationsPage.Value
    $nextLink = $applicationsPage.'@odata.nextLink'
} while ($nextLink)
 
# Query each application
foreach ($application in $allApplications) {
     
# Retrieve secrets
    $secretsUri = "https://graph.microsoft.com/v1.0/applications/$($application.id)/passwordCredentials"
    $secrets = Invoke-MgGraphRequest -Method GET -Uri $secretsUri
 
# Query secrets
    foreach ($secret in $secrets.value) {
        try {
            $expiryDateTime = [DateTime]$secret.endDateTime
            $expiryDate = $expiryDateTime.Date
 
            if ($expiryDate -ne $null) {
                $daysUntilExpiry = ($expiryDate - (Get-Date).Date).Days
				
				# Want a different time range? You can simply modify the condition below
                if ($daysUntilExpiry -le 30) {
                    Write-Host -ForegroundColor Red "Secret Expiring within a Month:"
                    Write-Host "Application Name: $($application.displayName)"
                    Write-Host "Application ID: $($application.id)"
                    Write-Host "  Key ID: $($secret.keyId)"
                    Write-Host "  Expiry Date: $($expiryDate.ToString("yyyy-MM-dd"))"
                    Write-Host "  Days Until Expiry: $daysUntilExpiry"
                }
            } else {
                throw "Invalid DateTime format"
            }
        }
        catch {
            Write-Host "Error parsing secret expiry date. Skipping secret."
        }
    }
    Write-Host
}
 
# Disconnect from Microsoft Graph
Disconnect-MgGraph

If you want a different time range, you can simply modify $daysUntilExpiry -le 30 in the code where 30 is the amount of days.

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

How to Fix a Newer Version of OneDrive is Installed in Windows 10/11

Next Post

Automatically Installs Microsoft Graph PowerShell Module if not Installed Already When Run a Script

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory