導出 Entra ID 應用程序註冊 證書和機密到期報告
您需要一種快速的方法來顯示所有 Entra ID 應用程序註冊證書或機密及其到期日期。如果您在此之前未續訂,則與應用程序的連接將不再有效。因此,重要的是不要讓應用程序註冊的證書或機密過期。在本文中,你將了解如何使用 Microsoft Graph PowerShell 將 Entra ID 應用註冊及其證書和客戶端密鑰到期日期導出到報告中。
檢查 Microsoft Entra 中的應用程序註冊證書和機密過期日期
要檢查應用程序註冊證書或密鑰是否已過期,請執行以下步驟:
- 登錄到微軟 Entra 管理中心
- 擴張身份 > 應用程序 > 應用程序註冊
- 選擇所有應用程序
- 單擊應用您想要檢查證書過期狀態的
在我們的示例中,我們有 4 個應用程序,應用程序 Kairo 顯示證書和機密已過期。
- 點擊證書和秘密
- 選擇證書或者客戶秘密
在我們的示例中,應用程序 Kairo 僅具有客戶端機密,沒有證書。一個客戶端密鑰已過期(紅色),另一個客戶端密鑰即將過期(橙色)。
要續訂 Microsoft Entra ID 中的客戶端密鑰,我們需要創建新的客戶端密鑰並刪除過期的客戶端密鑰。
瀏覽所有申請並檢查證書或客戶端密鑰的到期日期(包括其狀態)需要時間。因此,最好的方法是運行 PowerShell 腳本來創建包含所有信息的報告。
讓我們看看下一步的 PowerShell 腳本。
使用 PowerShell 獲取 Entra ID 應用程序註冊證書和機密到期日期
獲取所有應用程序註冊及其證書和機密過期日期的一個好方法是使用 Microsoft Graph PowerShell。
Get-AppCertSecStatus.ps1 腳本將獲取 Microsoft Entra ID 中的所有應用註冊並收集以下詳細信息:
- 應用程序名稱
- 應用程序ID
- 憑證名稱
- 登錄類型
- 創建日期時間
- 開始日期時間
- 結束日期時間
- 過期狀態
- 驗證類型
- 剩余天數
- 所有者
- 所有者ID
步驟 1. 準備 Get-AppCertSecStatus PowerShell 腳本
上創建兩個文件夾(中:)駕駛:
- 溫度
- 腳本
下載 Get-AppCertSecStatus.ps1 PowerShell 腳本並將其放入C:腳本文件夾。該腳本會將 CSV 文件導出到C:溫度文件夾。
確保文件未被阻止,以防止運行腳本時出現錯誤。請閱讀文章運行 PowerShell 腳本時出現未數字簽名錯誤來了解更多信息。
另一種選擇是將以下代碼複製並粘貼到記事本中。給它起個名字獲取AppCertSecStatus.ps1並將其放置在C:腳本文件夾。
<#
.SYNOPSIS
Get-AppCertSecStatus.ps1
.DESCRIPTION
Export Entra ID app registrations certificates and secrets expiration date.
.LINK
www.alitajran.com/export-entra-id-app-registrations-certificates-secrets/
.NOTES
Written by: ALI TAJRAN
Website: www.alitajran.com
LinkedIn: linkedin.com/in/alitajran
.CHANGELOG
V1.00, 01/14/2024 - Initial version
V1.10, 04/30/2024 - Added AppId property to the results
#>
# CSV file path to export
$CsvPath = "C:tempAppRegistrationsReport.csv"
# Connect to MgGraph with necessary scopes
Connect-MgGraph "Directory.Read.All", "Application.Read.All"
# Initialize a List to store the data
$Report = [System.Collections.Generic.List[Object]]::new()
# Get properties
$Properties = @(
'AppId',
'DisplayName',
'PasswordCredentials',
'KeyCredentials',
'Id',
'SignInAudience',
'CreatedDateTime'
)
# Get Applications
$Apps = Get-MgApplication -All -Property $Properties | Select-Object $Properties
foreach ($App in $Apps) {
# Get Owner information
$Owner = Get-MgApplicationOwner -All -ApplicationId $App.Id
$Username = if ($Owner) { $Owner.AdditionalProperties.userPrincipalName -join ';' } else { "N/A" }
$OwnerID = if ($Owner) { $Owner.Id -join ';' } else { "N/A" }
# Check application for client secret
if ($null -ne $App.PasswordCredentials) {
foreach ($Creds in $App.PasswordCredentials) {
# Calculate days left until expiration
$DaysLeft = ($Creds.EndDateTime - (Get-Date)).Days
# Create custom object for client secret results
$ReportLine = [PSCustomObject]@{
ApplicationName = $App.DisplayName
ApplicationID = $App.AppId
CredentialName = $Creds.DisplayName
SignInType = $App.SignInAudience
CreatedDateTime = $App.CreatedDateTime
StartDateTime = $Creds.StartDateTime
EndDateTime = $Creds.EndDateTime
ExpireStatus = if ($Creds.EndDateTime -lt (Get-Date)) { "Expired" } else { "Not expired" }
AuthType = "Client_Secret"
DaysLeft = $DaysLeft
Owner = $Username
OwnerID = $OwnerID
}
# Add the report line to the List
$Report.Add($ReportLine)
}
}
# Check application for certificate
if ($null -ne $App.KeyCredentials) {
foreach ($Cert in $App.KeyCredentials) {
# Calculate days left until expiration
$DaysLeft = ($Cert.EndDateTime - (Get-Date)).Days
# Create custom object for certificate results
$ReportLine = [PSCustomObject]@{
ApplicationName = $App.DisplayName
ApplicationID = $App.AppId
CredentialName = $Cert.DisplayName
SignInType = $App.SignInAudience
CreatedDateTime = $App.CreatedDateTime
StartDateTime = $Cert.StartDateTime
EndDateTime = $Cert.EndDateTime
ExpireStatus = if ($Cert.EndDateTime -lt (Get-Date)) { "Expired" } else { "Not expired" }
AuthType = "Certificate"
DaysLeft = $DaysLeft
Owner = $Username
OwnerID = $OwnerID
}
# Add the report line to the List
$Report.Add($ReportLine)
}
}
}
# Sort the results based on DaysLeft
$Report = $Report | Sort-Object -Property DaysLeft
# Export to CSV file
$Report | Export-Csv $CsvPath -Encoding utf8 -NoTypeInformation
# Out-GridView
$Report | Out-GridView -Title "Microsoft Entra ID app registrations report"
- 21號線:編輯 CSV 文件路徑
步驟 2. 安裝 Microsoft Graph PowerShell
以管理員身份運行 Windows PowerShell 並安裝 Microsoft Graph PowerShell。
Install-Module Microsoft.Graph -Force
重要的:在運行 cmdlet 或腳本之前,請務必更新到最新的 Microsoft Graph PowerShell 模塊版本,以防止出現錯誤和不正確的結果。
步驟 3. 運行 Get-AppCertSecStatus PowerShell 腳本
使用 PowerShell 獲取所有 Entra ID 應用程序註冊證書和機密到期日期,包括其狀態。
運行以下命令來運行腳本 Get-AppCertSecStatus.ps1。
c:scripts.Get-AppCertSecStatus.ps1
一個網格視圖將顯示您需要的所有信息。
步驟 4. 打開 Entra ID 應用程序註冊報告
Get-AppCertSecStatus.ps1 PowerShell 腳本將所有應用程序註冊及其證書和客戶端密鑰過期狀態導出到 CSV 文件。
找到文件應用程序註冊報告.csv在路徑中C:溫度。
使用您喜歡的應用程序打開 CSV 文件。在我們的示例中,它是 Microsoft Excel。
Entra ID 應用程序註冊證書和客戶端密鑰過期報告看起來很棒。
結論
您了解瞭如何使用 Microsoft Graph PowerShell 導出 Entra ID 應用註冊證書和機密。運行該腳本,它將收集所有信息並將其導出到 CSV 文件報告。確保在證書或客戶端密鑰過期之前續訂證書或客戶端密鑰,以便與應用程序的連接保持完整。
您喜歡這篇文章嗎?您可能還喜歡導出 Microsoft 365 非活動用戶報告。不要忘記關注我們並分享這篇文章。
