简体   繁体   中英

Check if IdentifierUris exists in Microsoft Azure Active Directory Application using PowerShell

I am using following PowerShell script to create Azure Active Directory Application

$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"
}

I am performing check for Display Name , I also need to perform check on IdentifierUris if it exists but cannot find any command. could anyone help

For this, I recommend using the Get-AzureADApplication cmdlet from the AzureAD PowerShell module (the cmdlets are of the form -AzureAD ), instead of the cmdlet from the Azure PowerShell 2.0 module (where the cmdlets are of the form -AzAD ).

With this cmdlet, you can specify a filter as you would in the Azure AD Graph API $filter parameter, and get what you're looking for in one request.

To get all Application objects with a given display name or at any identifier URI (technically it's a list) matching the one you've given, you can do the following:

$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!
}

Edit: If for some reason you must use the Azure PowerShell module (Az), then you will need to make two separate calls to check:

$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
}

I cannot test this myself, but this might help:

$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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM