Skip to content

Commit

Permalink
Correction of az ad app list in the case of an existing app + workaro…
Browse files Browse the repository at this point in the history
…und for an azd change - add azd login due to add env variables to config file
  • Loading branch information
rpothin committed Feb 12, 2023
1 parent e74f398 commit d8b1c3f
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions scripts/post-init-setup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,10 @@ if (!($response.ToLower() -eq "y")) {
$azureDeploymentAppRegistrationName = "sp-$azureDefaultEnvironmentName-azure"

Write-Verbose "Checking if an '$azureDeploymentAppRegistrationName' app registration already exist..."
$azureDeploymentAppRegistrationListResult = az ad app list --filter "displayName eq '$azureDeploymentAppRegistrationName'" --query '[].{id:id, appId:appId}' --output tsv
$azureDeploymentAppRegistrationListResult = az ad app list --filter "displayName eq '$azureDeploymentAppRegistrationName'" --query '[].{id:id, appId:appId}' --output json | ConvertFrom-Json

if (!$?) {
az ad app list --filter "displayName eq '$azureDeploymentAppRegistrationName'" --query '[].{id:id, appId:appId}' --output tsv
az ad app list --filter "displayName eq '$azureDeploymentAppRegistrationName'" --query '[].{id:id, appId:appId}' --output json | ConvertFrom-Json
Write-Error -Message "Error while trying to check if an app registration with the following name already exists: $azureDeploymentAppRegistrationName" -ErrorAction Stop
}

Expand All @@ -258,7 +258,7 @@ if ([string]::IsNullOrEmpty($azureDeploymentAppRegistrationListResult)) {
$azureDeploymentAppRegistrationId = $azureDeploymentAppRegistrationCreationResult[1]
Write-Verbose "👍🏼 '$azureDeploymentAppRegistrationName' app registration created!"
} else {
$azureDeploymentAppRegistrationId = $azureDeploymentAppRegistrationListResult[1]
$azureDeploymentAppRegistrationId = $azureDeploymentAppRegistrationListResult.appId
Write-Verbose "Existing '$azureDeploymentAppRegistrationName' app registration found."
}

Expand Down Expand Up @@ -337,10 +337,10 @@ if (!($response.ToLower() -eq "y")) {
$dataverseAppRegistrationName = "sp-$azureDefaultEnvironmentName-dataverse"

Write-Verbose "Checking if an '$dataverseAppRegistrationName' app registration already exist..."
$dataverseAppRegistrationListResult = az ad app list --filter "displayName eq '$dataverseAppRegistrationName'" --query '[].{id:id, appId:appId}' --output tsv
$dataverseAppRegistrationListResult = az ad app list --filter "displayName eq '$dataverseAppRegistrationName'" --query '[].{id:id, appId:appId}' --output json | ConvertFrom-Json

if (!$?) {
az ad app list --filter "displayName eq '$dataverseAppRegistrationName'" --query '[].{id:id, appId:appId}' --output tsv
az ad app list --filter "displayName eq '$dataverseAppRegistrationName'" --query '[].{id:id, appId:appId}' --output json | ConvertFrom-Json
Write-Error -Message "Error while trying to check if an app registration with the following name already exists: $dataverseAppRegistrationName" -ErrorAction Stop
}

Expand All @@ -356,7 +356,7 @@ if ([string]::IsNullOrEmpty($dataverseAppRegistrationListResult)) {
$dataverseAppRegistrationId = $dataverseAppRegistrationCreationResult[1]
Write-Verbose "👍🏼 '$dataverseAppRegistrationName' app registration created!"
} else {
$dataverseAppRegistrationId = $dataverseAppRegistrationListResult[1]
$dataverseAppRegistrationId = $dataverseAppRegistrationListResult.appId
Write-Verbose "Existing '$dataverseAppRegistrationName' app registration found."
}

Expand Down Expand Up @@ -400,6 +400,10 @@ if (![string]::IsNullOrEmpty($dataverseServicePrincipalPassword)) {
Write-Error -Message "Error during credendial reset for the '$dataverseAppRegistrationName' service principal." -ErrorAction Stop
}

# Run 'azd login' to be able to add new environment variable to env config file
Write-Host "Run 'azd login' to be able to add new environment variable to env config file - use the Azure account related to the considered subscription" -ForegroundColor Blue
azd login

# Add application registration name as an environment variable to the default environment
Write-Verbose "Add application registration name to the '.env' file of the default environment..."
azd env set DATAVERSE_SERVICE_PRINCIPAL_NAME $dataverseAppRegistrationName
Expand Down

0 comments on commit d8b1c3f

Please sign in to comment.