在相容模式下執行舊有腳本

本文將學習如何在 Microsoft Entra PowerShell 中使用相容模式執行舊有的 Azure AD PowerShell 腳本,實現無縫腳本遷移,只需最小的修改。 此流程讓您能順利過渡到新模組,同時維持現有自動化工作流程,確保持續效率並符合最新工具的合規性。

Microsoft Entra PowerShell 與 Azure AD PowerShell 模組的相容性超過 98%。 在相容模式下,你可以用 Microsoft Entra PowerShell 執行現有的 Azure AD PowerShell 腳本,只需使用Enable-EntraAzureADAlias指令即可。 若要在 Microsoft Entra PowerShell 中尋找 Azure AD PowerShell 和 MSOnline Cmdlet 的對應項,請使用 Azure AD PowerShell 與 Microsoft Entra PowerShell Cmdlet 對應表

請搭配 Enable-EntraAzureADAlias 使用相容模式

指令檔透過 Enable-EntraAzureADAlias 別名啟用相容模式。 預設情況下,Enable-EntraAzureADAlias僅啟用目前 Microsoft Entra PowerShell 會話的相容別名。 欲了解更多資訊,請參閱 Enable-EntraAzureADAlias 參考文件。

若要在現有的 AzureAD PowerShell 指令碼中使用 Microsoft Entra PowerShell,請以以下提供的三行取代 Connect-AzureAD 命令。 這三行是你遷移後 AzureAD PowerShell 腳本的起點。

Import-Module -Name Microsoft.Entra.Applications
Connect-Entra -Scopes 'Application.Read.All' #Replaces Connect-AzureAD for auth
Enable-EntraAzureADAlias #enable aliasing
Get-AzureADApplication -Top 2

Example

在這個例子中,你執行一個腳本,使用 Microsoft Entra PowerShell 匯出帶有過期秘密的應用程式。 此範例假設 Microsoft Entra PowerShell 模組已安裝

以下範例腳本為原始的 AzureAD PowerShell 腳本。

Connect-AzureAD
$applications = Get-AzureADApplication -All $true
$Logs = @()
Write-Host "I would like to see the Applications with the Secrets and Certificates that expire in the next X amount of Days? <<Replace X with the number of days. The answer should be ONLY in Numbers>>" -ForegroundColor Green
$Days = Read-Host

Write-Host "Would you like to see Applications with already expired secrets or certificates as well? <<Answer with [Yes] [No]>>" -ForegroundColor Green
$alreadyExpired = Read-Host

$now = Get-Date

foreach ($app in $applications) {
    $appName = $app.DisplayName
    $appID = $app.objectid
    $applID = $app.AppId
    $appCreds = Get-AzureADApplication -ObjectId $appID | Select-Object -Property PasswordCredentials, KeyCredentials
    $secret = $appCreds.PasswordCredentials
    $cert = $appCreds.KeyCredentials

註: 此程式碼片段為便於閱讀而被簡化。 詳情請參閱 完整範例

若要讓您的指令碼搭配 Microsoft Entra PowerShell 模組使用,請將 Connect-AzureAD cmdlet 替換為程式碼片段中提供的三行。 你不需要重寫整個劇本。

以下文字是遷移後的文字。

Import-Module -Name Microsoft.Entra.Users
Connect-Entra #Replaces Connect-AzureAD for auth
Enable-EntraAzureADAlias #Activate aliasing

$applications = Get-AzureADApplication -All $true
$logs = @()
Write-Host "I would like to see the Applications with the Secrets and Certificates that expire in the next X amount of Days? <<Replace X with the number of days. The answer should be ONLY in Numbers>>" -ForegroundColor Green
$days = Read-Host
Write-Host "Would you like to see Applications with already expired secrets or certificates as well? <<Answer with [Yes] [No]>>" -ForegroundColor Green
$alreadyExpired = Read-Host
$now = Get-Date
foreach ($app in $applications) {
    $appName = $app.DisplayName
    $appID = $app.Objectid
    $applID = $app.AppId
    $appCreds = Get-AzureADApplication -ObjectId $appID | Select-Object -Property PasswordCredentials, KeyCredentials
    $secret = $appCreds.PasswordCredentials
    $cert = $appCreds.KeyCredentials

註: 此程式碼片段為便於閱讀而被簡化。 詳情請參閱完整的修改版範例

測試與 Test-EntraScript 指令的相容性

Test-EntraScript cmdlet 用來驗證一個帶有 Azure AD PowerShell 指令的腳本是否能與 Microsoft Entra PowerShell 模組相容。 若有相容性問題,會列出相關資料,包括行號、問題類型、不相容指令及特定程式碼片段。

已知問題

從 Azure AD PowerShell 模組遷移到 Microsoft Entra PowerShell 時,可能會遇到幾個已知問題。

  • -Filter參數可能無法正常運作。
  • -SearchString參數可能無法正常運作。
  • 輸出物件與 AzureAD 的輸出物件可能略有不同。