使用PowerShell检查Microsoft Azure Active Directory应用程序中是否存在IdentifierUris

时间:2019-05-09 12:23:43

标签: powershell azure-active-directory azure-powershell

我正在使用以下PowerShell脚本创建Azure Active Directory应用程序

$appName = "data-factory-app"
$appURI = "www.datafactoryapp.com"
$appExists = Get-AzADApplication -DisplayName $appName
if (-not $appExists)
{
 if (-not $appExists.IdentifierUris
 New-AzADApplication -DisplayName $appName -IdentifierUris $appURI
}
else 
{
 Write-Output "Application Already Exists"
}

我正在对Display Name进行检查,我还需要对IdentifierUris进行检查,如果存在但找不到任何命令。谁能帮忙

2 个答案:

答案 0 :(得分:1)

为此,我建议使用AzureAD PowerShell模块中的Get-AzureADApplication cmdlet(cmdlet的形式为 -AzureAD ),而不要使用Azure PowerShell 2.0模块中的cmdlet( cmdlet的格式为 -AzAD )。

使用此cmdlet,可以像在Azure AD Graph API $filter参数中那样指定过滤器,并在一个请求中获得所需的内容。

要获得与给定显示名称​​或匹配的任何标识符URI(从技术上来说是一个列表)的所有Application对象,都可以执行以下操作:

$appName = "data-factory-app"
$appURI  = "www.datafactoryapp.com"
$filter  = "displayName eq '{0}' or identifierUris/any(u:u eq '{1}')" -f $appName, $appURI
$appExists = Get-AzureADApplication -Filter $filter

if (-not $appExists) {
     # No application exists with that display name or identifier URI
} else {
     # An application already exists with that display name or identifier URI!
}

编辑:如果出于某些原因必须使用Azure PowerShell模块(Az),则需要进行两个单独的调用来进行检查:

$appName = "data-factory-app"
$appURI  = "www.datafactoryapp.com"

$appExistsWithDisplayName = Get-AzADApplication -DisplayName $appName
if (-not $appExistsWithDisplayName) {

    $appExistsWithIdentifierUri = Get-AzADApplication -IdentifierUri $appURI
    if (-not $appExistsWithIdentifierUri)) {
        # No application exists with that display name or identifier URI
    } else {
        # An application already exists with that identifier URI
    }
} else {
     # An application already exists with that display name
}

答案 1 :(得分:0)

我自己无法测试,但这可能会有所帮助:

$appName   = "data-factory-app"
$appURI    = "www.datafactoryapp.com"
$appExists = Get-AzADApplication -DisplayName $appName

if (-not $appExists) {
    Write-Output "Application '$appName' does not exist"
    # create it here?
    # see https://docs.microsoft.com/en-us/powershell/module/az.resources/new-azadapplication?view=azps-2.0.0
}
else {
    Write-Output "Application already exists, checking IdentifierUris"
    if (-not $appExists.IdentifierUris -or @($appExists.IdentifierUris) -notcontains $appURI ) {
        Write-Output "Updating Application IdentifierUris"
        $appExists | Update-AzADApplication -IdentifierUri $appURI
    }
}