Skip to content

Commit aa2d53a

Browse files
committed
Add step tracking and retries to API app setup
Introduced a $Step variable to track progress during API app creation and included the step in error logs for better troubleshooting. Added -maxRetries 3 to key New-GraphPOSTRequest calls to improve reliability of application password, identifier URI, and service principal creation.
1 parent 5c4fe80 commit aa2d53a

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

Modules/CIPPCore/Public/Authentication/New-CIPPAPIConfig.ps1

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,23 @@ function New-CIPPAPIConfig {
6363
if ($PSCmdlet.ShouldProcess($AppName, 'Create API App')) {
6464
Write-Information 'Creating app'
6565
Write-Information $CreateBody
66+
$Step = 'Creating Application'
6667
$APIApp = New-GraphPOSTRequest -uri 'https://graph.microsoft.com/v1.0/applications' -AsApp $true -NoAuthCheck $true -type POST -body $CreateBody
6768
Write-Information 'Creating password'
68-
$APIPassword = New-GraphPOSTRequest -uri "https://graph.microsoft.com/v1.0/applications/$($APIApp.id)/addPassword" -AsApp $true -NoAuthCheck $true -type POST -body "{`"passwordCredential`":{`"displayName`":`"Generated by API Setup`"}}"
69+
$Step = 'Creating Application Password'
70+
$APIPassword = New-GraphPOSTRequest -uri "https://graph.microsoft.com/v1.0/applications/$($APIApp.id)/addPassword" -AsApp $true -NoAuthCheck $true -type POST -body "{`"passwordCredential`":{`"displayName`":`"Generated by API Setup`"}}" -maxRetries 3
6971
Write-Information 'Adding App URL'
70-
$APIIdUrl = New-GraphPOSTRequest -uri "https://graph.microsoft.com/v1.0/applications/$($APIApp.id)" -AsApp $true -NoAuthCheck $true -type PATCH -body "{`"identifierUris`":[`"api://$($APIApp.appId)`"]}"
72+
$Step = 'Adding Application Identifier URI'
73+
$APIIdUrl = New-GraphPOSTRequest -uri "https://graph.microsoft.com/v1.0/applications/$($APIApp.id)" -AsApp $true -NoAuthCheck $true -type PATCH -body "{`"identifierUris`":[`"api://$($APIApp.appId)`"]}" -maxRetries 3
7174
Write-Information 'Adding serviceprincipal'
72-
$ServicePrincipal = New-GraphPOSTRequest -uri 'https://graph.microsoft.com/v1.0/serviceprincipals' -AsApp $true -NoAuthCheck $true -type POST -body "{`"accountEnabled`":true,`"appId`":`"$($APIApp.appId)`",`"displayName`":`"$AppName`",`"tags`":[`"WindowsAzureActiveDirectoryIntegratedApp`",`"AppServiceIntegratedApp`"]}"
75+
$Step = 'Creating Service Principal'
76+
$ServicePrincipal = New-GraphPOSTRequest -uri 'https://graph.microsoft.com/v1.0/serviceprincipals' -AsApp $true -NoAuthCheck $true -type POST -body "{`"accountEnabled`":true,`"appId`":`"$($APIApp.appId)`",`"displayName`":`"$AppName`",`"tags`":[`"WindowsAzureActiveDirectoryIntegratedApp`",`"AppServiceIntegratedApp`"]}" -maxRetries 3
7377
Write-LogMessage -headers $Headers -API $APINAME -tenant 'None '-message "Created CIPP-API App with name '$($APIApp.displayName)'." -Sev 'info'
7478
}
7579
}
7680
if ($ResetSecret.IsPresent -and $APIApp) {
7781
if ($PSCmdlet.ShouldProcess($APIApp.displayName, 'Reset API Secret')) {
82+
$Step = 'Resetting Application Password'
7883
Write-Information 'Removing all old passwords'
7984
$Requests = @(
8085
@{
@@ -119,7 +124,7 @@ function New-CIPPAPIConfig {
119124
} catch {
120125
$ErrorMessage = Get-CippException -Exception $_
121126
Write-Information ($ErrorMessage | ConvertTo-Json -Depth 10)
122-
Write-LogMessage -headers $Headers -API $APINAME -tenant 'None' -message "Failed to setup CIPP-API Access: $($ErrorMessage.NormalizedError) Linenumber: $($_.InvocationInfo.ScriptLineNumber)" -Sev 'Error' -LogData $ErrorMessage
127+
Write-LogMessage -headers $Headers -API $APINAME -tenant 'None' -message "CIPP-API Setup failed at step ($Step): $($ErrorMessage.NormalizedError) Linenumber: $($_.InvocationInfo.ScriptLineNumber)" -Sev 'Error' -LogData $ErrorMessage
123128
throw "Failed to setup CIPP-API Access: $($ErrorMessage.NormalizedError)"
124129
}
125130
}

0 commit comments

Comments
 (0)