本文介绍如何使用兼容性模式在 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 cmdlet 通过别名启用兼容性模式。 默认情况下,Enable-EntraAzureADAlias 仅对当前的 Microsoft Entra PowerShell 会话启用兼容性别名。 有关详细信息,请参阅 Enable-EntraAzureADAlias 参考文档。
若要将 Microsoft Entra PowerShell 与现有 AzureAD 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
示例
在此示例中,运行一个脚本,该脚本使用 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 输出对象略有不同。