<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>get application cert expiration date graph api Archives - the Sysadmin Channel</title>
	<atom:link href="https://thesysadminchannel.com/tag/get-application-cert-expiration-date-graph-api/feed/" rel="self" type="application/rss+xml" />
	<link>https://thesysadminchannel.com/tag/get-application-cert-expiration-date-graph-api/</link>
	<description>Documenting My Life as a System Administrator</description>
	<lastBuildDate>Tue, 17 Jan 2023 01:51:27 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
<site xmlns="com-wordpress:feed-additions:1">144174110</site>	<item>
		<title>Get Application Certificate and Secret Expiration with Graph API</title>
		<link>https://thesysadminchannel.com/get-application-certificate-and-secret-expiration-with-graph-api-powershell/</link>
					<comments>https://thesysadminchannel.com/get-application-certificate-and-secret-expiration-with-graph-api-powershell/#comments</comments>
		
		<dc:creator><![CDATA[Paul Contreras]]></dc:creator>
		<pubDate>Tue, 17 Jan 2023 01:51:27 +0000</pubDate>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Graph API]]></category>
		<category><![CDATA[azure app cert expiration PowerShell]]></category>
		<category><![CDATA[get application cert expiration date graph api]]></category>
		<category><![CDATA[Get Application Certificate and Secret Expiration with Graph API]]></category>
		<category><![CDATA[get secret expiration date powershell]]></category>
		<category><![CDATA[get secrete expiration graph api]]></category>
		<guid isPermaLink="false">https://thesysadminchannel.com/?p=4676</guid>

					<description><![CDATA[<p>In the world of Azure cloud automation we always need to ensure that our accounts are able to properly authenticate. Accounts with username and password might have Active Directory alert you when your password expires, however, what can we use&#8230; <a href="https://thesysadminchannel.com/get-application-certificate-and-secret-expiration-with-graph-api-powershell/" class="more-link">Continue Reading <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a href="https://thesysadminchannel.com/get-application-certificate-and-secret-expiration-with-graph-api-powershell/">Get Application Certificate and Secret Expiration with Graph API</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In the world of Azure cloud automation we always need to ensure that our accounts are able to properly authenticate.  Accounts with username and password might have Active Directory alert you when your password expires, however, what can we use to ensure the secrets or certificates tied to an App registration aren&#8217;t nearing expiration ( or worse, already expired).  Today I am going to share a PowerShell script that shows you how to get application certificate and secret expiration with Graph API.</p>
<div id="tableofcontents">
<h2>Table Of Contents</h2>
<ul>
<li><a href="#requirements">Requirements</a></li>
<li><a href="#azureportal">Check an App Registration for Expired Keys in Azure Portal</a></li>
<li><a href="#powershell">Get Application Certificate and Secret Expiration with Graph API</a></li>
<ul>
<li><a href="#powershellscript">PowerShell Script</a></li>
<li><a href="#scriptparameters">Script Parameters</a></li>
<li><a href="#examples">Examples and Usage</a></li>
</ul>
<li><a href="#conclusion">Conclusion</a></li>
</ul>
</div>
<div id="requirements" style="scroll-margin-top: 15px;"></div>
<h2>Requirements</h2>
<p>In order to run this, there are a few things that need to be in place to ensure we don&#8217;t run into any errors. Let&#8217;s touch on those item now.<br />
&nbsp;</p>
<ul>
<li>Directory.Read.All Permissions</li>
<li>Application.Read.All Permissions</li>
<li>Microsoft.Graph PowerShell SDK Module</li>
</ul>
<div id="azureportal" style="scroll-margin-top: 15px;"></div>
<h2>Check an App Registration for Expired Keys in Azure Portal</h2>
<p>Before we get into the PowerShell script, let&#8217;s take a look at how to check this manually so we know exactly what to expect when looking at the results of the script.<br />
&nbsp;</p>
<p>Within Azure AD:</p>
<ul>
<li>Navigate to <a href="https://portal.azure.com/#view/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/~/RegisteredApps" rel="noopener" target="_blank">App Registrations</a></li>
<li>Select an App that has a certificate or secret added</li>
</ul>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2023/01/Azure-portal-expired-keys.jpg" target="_blank" rel="noopener"><img fetchpriority="high" decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2023/01/Azure-portal-expired-keys.jpg" alt="Azure portal expired keys" width="1108" height="352" class="aligncenter size-full wp-image-4684" srcset="https://thesysadminchannel.com/wp-content/uploads/2023/01/Azure-portal-expired-keys.jpg?v=1673896287 1108w, https://thesysadminchannel.com/wp-content/uploads/2023/01/Azure-portal-expired-keys-1024x325.jpg?v=1673896287 1024w, https://thesysadminchannel.com/wp-content/uploads/2023/01/Azure-portal-expired-keys-768x244.jpg?v=1673896287 768w" sizes="(max-width: 1108px) 100vw, 1108px" /></a><br />
&nbsp;</p>
<ul>
<li>Go to Certificates &#038; secrets blade</li>
</ul>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2023/01/Certificate-Expiration.jpg" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2023/01/Certificate-Expiration.jpg" alt="Certificate Expiration" width="1059" height="294" class="aligncenter size-full wp-image-4685" srcset="https://thesysadminchannel.com/wp-content/uploads/2023/01/Certificate-Expiration.jpg?v=1673896397 1059w, https://thesysadminchannel.com/wp-content/uploads/2023/01/Certificate-Expiration-1024x284.jpg?v=1673896397 1024w, https://thesysadminchannel.com/wp-content/uploads/2023/01/Certificate-Expiration-768x213.jpg?v=1673896397 768w" sizes="(max-width: 1059px) 100vw, 1059px" /></a><br />
&nbsp;</p>
<p>Here we can see the status of the certificate for this specific application. Specifically, we&#8217;re interested in the expiration date, certificate thumbprint and Key ID (certificate ID).<br />
&nbsp;</p>
<p>While the portal does give you a visual of when keys are expiring, it still requires you to take time out of your day to manually check.  Let&#8217;s take a look at how we can accomplish the same thing automatically using the PowerShell script I wrote.</p>
<div id="powershell" style="scroll-margin-top: 15px;"></div>
<h2>Get Application Certificate and Secret Expiration with Graph API PowerShell</h2>
<p>Now that we&#8217;ve gone over the manual method, let&#8217;s use PowerShell and Graph API to our advantage and show the same information in an automated fashion.  Since we know how to <a href="https://thesysadminchannel.com/automate-powershell-scripts-with-task-scheduler/" rel="noopener" target="_blank">automate Powershell Scripts With Task Scheduler</a>, we can schedule this on a daily basis and let it alert you without any additional effort on your end.<br />
&nbsp;</p>
<div id="powershellscript" style="scroll-margin-top: 15px;"></div>
<p>Now for the PowerShell script:</p>
<pre class="brush: powershell; title: ; notranslate">
Function Get-MgApplicationCertificateAndSecretExpiration {
&lt;#
.SYNOPSIS
    This will display all Applications that have certificates or secrets expiring within a certain timeframe


.NOTES
    Name: Get-MgApplicationCertificateAndSecretExpiration
    Author: Paul Contreras
    Version: 1.3
    DateCreated: 2022-Feb-8

.LINK
    https://thesysadminchannel.com/get-application-certificate-and-secret-expiration-with-graph-api-powershell -

.EXAMPLE
    Get-MgApplicationCertificateAndSecretExpiration

.EXAMPLE
    Get-MgApplicationCertificateAndSecretExpiration -ShowExpiredKeys
#&gt;

    [CmdletBinding(DefaultParameterSetName='Default')]
    param(
        [Parameter(
            Mandatory = $false,
            ParameterSetName = 'CertOnly'
        )]
        [switch]    $ShowOnlyCertificates,

        [Parameter(
            Mandatory = $false,
            ParameterSetName = 'SecretOnly'
        )]
        [switch]    $ShowOnlySecrets,


        [Parameter(
            Mandatory = $false
        )]
        [switch]    $ShowExpiredKeys,


        [Parameter(
            Mandatory = $false
        )]
        [ValidateRange(1,720)]
        [int]    $DaysWithinExpiration = 30,


        [Parameter(
            Mandatory = $false,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [Alias('ApplicationId', 'ClientId')]
        [string]    $AppId
    )

    BEGIN {
        $ConnectionGraph = Get-MgContext
        if (-not $ConnectionGraph) {
            Write-Error &quot;Please connect to Microsoft Graph&quot; -ErrorAction Stop
        }
        #Adding an extra day to account for hour differences and offsets.
        $DaysWithinExpiration++
    }

    PROCESS {
        try {
            if ($PSBoundParameters.ContainsKey('AppId')) {
                $ApplicationList = Get-MgApplication -Filter &quot;AppId eq '$AppId'&quot; -ErrorAction Stop
                $AppFilter = $true
            } else {
                $ApplicationList = Get-MgApplication -All -Property AppId, DisplayName, PasswordCredentials, KeyCredentials, Id -PageSize 999 -ErrorAction Stop
            }

            #If certs are selected, show certs
            if ($PSBoundParameters.ContainsKey('ShowOnlyCertificates') -or

                #If neither Certs or Secrets are selected show both.
               (-not $PSBoundParameters.ContainsKey('ShowOnlyCertificates') -and
                -not $PSBoundParameters.ContainsKey('ShowOnlySecrets'))) {

                    $CertificateApps  = $ApplicationList | Where-Object {$_.keyCredentials}

                    $CertApp = foreach ($App in $CertificateApps) {
                        foreach ($Cert in $App.keyCredentials) {
                            if ( $Cert.endDateTime -le (Get-Date).AddDays($DaysWithinExpiration) -or ($AppFilter) ) {
                                [PSCustomObject]@{
                                    AppDisplayName      = $App.DisplayName
                                    AppId               = $App.AppId
                                    KeyType             = 'Certificate'
                                    ExpirationDate      = $Cert.EndDateTime
                                    DaysUntilExpiration = (($Cert.EndDateTime) - (Get-Date) | select -ExpandProperty TotalDays) -as [int]
                                    ThumbPrint          = [System.Convert]::ToBase64String($Cert.CustomKeyIdentifier)
                                    Id                  = $App.Id
                                    KeyId               = $Cert.KeyId
                                    Description         = $Cert.DisplayName
                                }
                            }
                        }
                    }

                    if ($PSBoundParameters.ContainsKey('ShowExpiredKeys')) {
                        $CertApp | Sort-Object DaysUntilExpiration
                    } else {
                        $CertApp | Sort-Object DaysUntilExpiration | Where-Object {$_.DaysUntilExpiration -ge 0}
                    }
            }

            #If secrets are selected, show secrets
            if ($PSBoundParameters.ContainsKey('ShowOnlySecrets') -or

                #If neither Certs or Secrets are selected show both.
               (-not $PSBoundParameters.ContainsKey('ShowOnlySecrets') -and
                -not $PSBoundParameters.ContainsKey('ShowOnlyCertificates'))) {

                    $ClientSecretApps = $ApplicationList | Where-Object {$_.passwordCredentials}

                    $SecretApp = foreach ($App in $ClientSecretApps){
                        foreach ($Secret in $App.PasswordCredentials) {
                            if ( $Secret.EndDateTime -le (Get-Date).AddDays($DaysWithinExpiration) -or ($AppFilter) ) {
                                [PSCustomObject]@{
                                    AppDisplayName      = $App.DisplayName
                                    AppId               = $App.AppId
                                    KeyType             = 'ClientSecret'
                                    ExpirationDate      = $Secret.EndDateTime
                                    DaysUntilExpiration = (($Secret.EndDateTime) - (Get-Date) | select -ExpandProperty TotalDays) -as [int]
                                    ThumbPrint          = 'N/A'
                                    Id                  = $App.Id
                                    KeyId               = $Secret.KeyId
                                    Description         = $Secret.DisplayName
                                }
                            }
                        }
                    }

                    if ($PSBoundParameters.ContainsKey('ShowExpiredKeys')) {
                        $SecretApp | Sort-Object DaysUntilExpiration
                    } else {
                        $SecretApp | Sort-Object DaysUntilExpiration | Where-Object {$_.DaysUntilExpiration -ge 0}
                    }
            }
        } catch {
            Write-Error $_.Exception.Message
        }
    }

    END {}
}
</pre>
<div id="scriptparameters" style="scroll-margin-top: 15px;"></div>
<h2>Script Parameters</h2>
<h3>    No Parameters</h3>
<p>DataType: N/A<br />
Description: Gather all apps and display the ones that have a secret or certificate expiring within 30 days.<br />
&nbsp;</p>
<h3>    -ShowOnlyCertificates</h3>
<p>DataType: switch<br />
Description: Only display certificates in the output. Expired keys and secrets will not be shown.<br />
&nbsp;</p>
<h3>    -ShowOnlySecrets</h3>
<p>DataType: switch<br />
Description: Only display secrets in the output. Expired keys and certificates will not be shown.<br />
&nbsp;</p>
<h3>    -ShowExpiredKeys</h3>
<p>DataType: switch<br />
Description: Display certificates or secrets are near expiration or have already expired.<br />
&nbsp;</p>
<h3>    -DaysWithinExpiration</h3>
<p>DataType: integer<br />
Description: Set the time frame to include expiring keys. This is defaulted to 30 days.<br />
&nbsp;</p>
<h3>    -AppId</h3>
<p>DataType: string<br />
Description: Specify an AppId (client Id) to see that specific applications keys.<br />
&nbsp;</p>
<div id="examples" style="scroll-margin-top: 15px;"></div>
<h3>Example 1 &#8211; Calling the script with no parameters</h3>
<p>Since this is a function, you&#8217;ll need to dot source it to load it into memory.  Assuming the file is your desktop you can run.</p>
<pre class="brush: powershell; title: ; notranslate">
. $Home\Desktop\Get-MgApplicationCertificateAndSecretExpiration.ps1
Get-MgApplicationCertificateAndSecretExpiration

</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2023/01/No-Parameters.jpg" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2023/01/No-Parameters.jpg" alt="No Parameters" width="902" height="603" class="aligncenter size-full wp-image-4692" srcset="https://thesysadminchannel.com/wp-content/uploads/2023/01/No-Parameters.jpg?v=1673900545 902w, https://thesysadminchannel.com/wp-content/uploads/2023/01/No-Parameters-768x513.jpg?v=1673900545 768w" sizes="(max-width: 902px) 100vw, 902px" /></a><br />
&nbsp;</p>
<p>Using the default output, take a look at the KeyType, ExpirationDate, DaysUntilExpiration and ThumbPrint.  Certificates will display a thumbprint while secrets will not.<br />
&nbsp;</p>
<h3>Example 2 &#8211; Display Only Certificates that are expired or nearing expiration</h3>
<pre class="brush: powershell; title: ; notranslate">
Get-MgApplicationCertificateAndSecretExpiration -ShowOnlyCertificates -ShowExpiredKeys `
| Select-Object AppDisplayName, KeyType, ExpirationDate, DaysUntilExpiration, KeyId, ThumbPrint
</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2023/01/Certificates-and-Expired-Keys.jpg" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2023/01/Certificates-and-Expired-Keys.jpg" alt="Certificates and Expired Keys" width="1015" height="466" class="aligncenter size-full wp-image-4694" srcset="https://thesysadminchannel.com/wp-content/uploads/2023/01/Certificates-and-Expired-Keys.jpg?v=1673901771 1015w, https://thesysadminchannel.com/wp-content/uploads/2023/01/Certificates-and-Expired-Keys-768x353.jpg?v=1673901771 768w" sizes="(max-width: 1015px) 100vw, 1015px" /></a><br />
&nbsp;</p>
<p>If you recall <a href="https://thesysadminchannel.com/wp-content/uploads/2023/01/Certificate-Expiration.webp" rel="noopener" target="_blank">Portal screenshot above</a>, our &#8220;App Automation 1&#8221; application had an expired cert with the thumbprint starting in &#8220;2E8972E&#8221; and the Key ID (certificate ID) started with &#8220;6f395fd4&#8221;.  This is the same information we can pull in PowerShell<br />
&nbsp;</p>
<h3>Example 3 &#8211; Display Only secrets that are expiring within 5 days</h3>
<pre class="brush: powershell; title: ; notranslate">
Get-MgApplicationCertificateAndSecretExpiration -ShowOnlySecrets -DaysWithinExpiration 5
| Select-Object AppDisplayName, KeyType, ExpirationDate, DaysUntilExpiration
</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2023/01/Show-only-Secrets.png" target="_blank" rel="noopener"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2023/01/Show-only-Secrets.png" alt="Show only Secrets" width="989" height="271" class="aligncenter size-full wp-image-4696" srcset="https://thesysadminchannel.com/wp-content/uploads/2023/01/Show-only-Secrets.png?v=1673902300 989w, https://thesysadminchannel.com/wp-content/uploads/2023/01/Show-only-Secrets-768x210.png?v=1673902300 768w" sizes="(max-width: 989px) 100vw, 989px" /></a></p>
<div id="conclusion" style="scroll-margin-top: 15px;"></div>
<h2>Conclusion</h2>
<p>Hopefully this article was able to show you get an application certificate and secret expiration with Graph API.  This is super useful to keep in your automation toolkit since the Azure world is now moving everything to Microsoft Graph.</p>
<p>The post <a href="https://thesysadminchannel.com/get-application-certificate-and-secret-expiration-with-graph-api-powershell/">Get Application Certificate and Secret Expiration with Graph API</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://thesysadminchannel.com/get-application-certificate-and-secret-expiration-with-graph-api-powershell/feed/</wfw:commentRss>
			<slash:comments>7</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4676</post-id>	</item>
	</channel>
</rss>
