@@ -13,7 +13,7 @@ function Invoke-ExecApiClient {
1313
1414 switch ($Action ) {
1515 ' List' {
16- $Apps = Get-CIPPAzDataTableEntity @Table
16+ $Apps = Get-CIPPAzDataTableEntity @Table | Where-Object { ! [ string ]::IsNullOrEmpty( $_ .RowKey ) }
1717 if (! $Apps ) {
1818 $Apps = @ ()
1919 } else {
@@ -31,6 +31,7 @@ function Invoke-ExecApiClient {
3131 ' AddUpdate' {
3232 if ($Request.Body.ClientId -or $Request.Body.AppName ) {
3333 $ClientId = $Request.Body.ClientId.value ?? $Request.Body.ClientId
34+ $AddUpdateSuccess = $false
3435 try {
3536 $ApiConfig = @ {
3637 Headers = $Request.Headers
@@ -46,8 +47,9 @@ function Invoke-ExecApiClient {
4647
4748 $ClientId = $APIConfig.ApplicationID
4849 $AddedText = $APIConfig.Results
50+ $AddUpdateSuccess = $true
4951 } catch {
50- $AddedText = ' Could not modify App Registrations. Check the CIPP documentation for API requirements.'
52+ $AddedText = " Could not modify App Registrations. Check the CIPP documentation for API requirements. Error: $ ( $_ .Exception.Message ) "
5153 $Body = $Body | Select-Object * - ExcludeProperty CIPPAPI
5254 }
5355 }
@@ -64,32 +66,38 @@ function Invoke-ExecApiClient {
6466 $IpRange = @ ()
6567 }
6668
67- $ExistingClient = Get-CIPPAzDataTableEntity @Table - Filter " RowKey eq '$ ( $ClientId ) '"
68- if ($ExistingClient ) {
69- $Client = $ExistingClient
70- $Client.Role = [string ]$Request.Body.Role.value
71- $Client.IPRange = " $ ( @ ($IpRange ) | ConvertTo-Json - Compress) "
72- $Client.Enabled = $Request.Body.Enabled ?? $false
73- Write-LogMessage - headers $Request.Headers - API ' ExecApiClient' - message " Updated API client $ ( $Request.Body.ClientId ) " - Sev ' Info'
74- $Results = ' API client updated'
75- } else {
76- $Client = @ {
77- ' PartitionKey' = ' ApiClients'
78- ' RowKey' = " $ ( $ClientId ) "
79- ' AppName' = " $ ( $APIConfig.AppName ?? $Request.Body.ClientId.addedFields.displayName ) "
80- ' Role' = [string ]$Request.Body.Role.value
81- ' IPRange' = " $ ( @ ($IpRange ) | ConvertTo-Json - Compress) "
82- ' Enabled' = $Request.Body.Enabled ?? $false
69+ if (! $AddUpdateSuccess -and ! $ClientId ) {
70+ $Body = @ {
71+ Results = $AddedText
8372 }
84- $Results = @ {
85- resultText = " API Client created with the name '$ ( $Client.AppName ) '. Use the Copy to Clipboard button to retrieve the secret."
86- copyField = $APIConfig.ApplicationSecret
87- state = ' success'
73+ } else {
74+ $ExistingClient = Get-CIPPAzDataTableEntity @Table - Filter " RowKey eq '$ ( $ClientId ) '"
75+ if ($ExistingClient ) {
76+ $Client = $ExistingClient
77+ $Client.Role = [string ]$Request.Body.Role.value
78+ $Client.IPRange = " $ ( @ ($IpRange ) | ConvertTo-Json - Compress) "
79+ $Client.Enabled = $Request.Body.Enabled ?? $false
80+ Write-LogMessage - headers $Request.Headers - API ' ExecApiClient' - message " Updated API client $ ( $Request.Body.ClientId ) " - Sev ' Info'
81+ $Results = ' API client updated'
82+ } else {
83+ $Client = @ {
84+ ' PartitionKey' = ' ApiClients'
85+ ' RowKey' = " $ ( $ClientId ) "
86+ ' AppName' = " $ ( $APIConfig.AppName ?? $Request.Body.ClientId.addedFields.displayName ) "
87+ ' Role' = [string ]$Request.Body.Role.value
88+ ' IPRange' = " $ ( @ ($IpRange ) | ConvertTo-Json - Compress) "
89+ ' Enabled' = $Request.Body.Enabled ?? $false
90+ }
91+ $Results = @ {
92+ resultText = " API Client created with the name '$ ( $Client.AppName ) '. Use the Copy to Clipboard button to retrieve the secret."
93+ copyField = $APIConfig.ApplicationSecret
94+ state = ' success'
95+ }
8896 }
89- }
9097
91- Add-CIPPAzDataTableEntity @Table - Entity $Client - Force | Out-Null
92- $Body = @ ($Results )
98+ Add-CIPPAzDataTableEntity @Table - Entity $Client - Force | Out-Null
99+ $Body = @ ($Results )
100+ }
93101 }
94102 ' GetAzureConfiguration' {
95103 $RGName = $ENV: WEBSITE_RESOURCE_GROUP
@@ -110,7 +118,7 @@ function Invoke-ExecApiClient {
110118 $TenantId = $ENV: TenantId
111119 $RGName = $ENV: WEBSITE_RESOURCE_GROUP
112120 $FunctionAppName = $ENV: WEBSITE_SITE_NAME
113- $AllClients = Get-CIPPAzDataTableEntity @Table - Filter ' Enabled eq true'
121+ $AllClients = Get-CIPPAzDataTableEntity @Table - Filter ' Enabled eq true' | Where-Object { ! [ string ]::IsNullOrEmpty( $_ .RowKey ) }
114122 $ClientIds = $AllClients.RowKey
115123 try {
116124 Set-CippApiAuth - RGName $RGName - FunctionAppName $FunctionAppName - TenantId $TenantId - ClientIds $ClientIds
0 commit comments