|
| 1 | +function Invoke-ExecDomainAction { |
| 2 | + <# |
| 3 | + .FUNCTIONALITY |
| 4 | + Entrypoint |
| 5 | + .ROLE |
| 6 | + Tenant.Administration.ReadWrite |
| 7 | + #> |
| 8 | + [CmdletBinding()] |
| 9 | + param($Request, $TriggerMetadata) |
| 10 | + |
| 11 | + $APIName = $Request.Params.CIPPEndpoint |
| 12 | + $TenantFilter = $Request.Body.tenantFilter |
| 13 | + $DomainName = $Request.Body.domain |
| 14 | + $Action = $Request.Body.Action |
| 15 | + |
| 16 | + try { |
| 17 | + if ([string]::IsNullOrWhiteSpace($DomainName)) { |
| 18 | + throw 'Domain name is required' |
| 19 | + } |
| 20 | + |
| 21 | + if ([string]::IsNullOrWhiteSpace($Action)) { |
| 22 | + throw 'Action is required' |
| 23 | + } |
| 24 | + |
| 25 | + switch ($Action) { |
| 26 | + 'verify' { |
| 27 | + Write-Information "Verifying domain $DomainName for tenant $TenantFilter" |
| 28 | + |
| 29 | + $Body = @{ |
| 30 | + verificationDnsRecordCollection = @() |
| 31 | + } | ConvertTo-Json -Compress |
| 32 | + |
| 33 | + $null = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/domains/$DomainName/verify" -tenantid $TenantFilter -type POST -body $Body -AsApp $true |
| 34 | + |
| 35 | + $Result = @{ |
| 36 | + resultText = "Domain $DomainName has been verified successfully." |
| 37 | + state = 'success' |
| 38 | + } |
| 39 | + |
| 40 | + Write-LogMessage -headers $Request.Headers -API $APIName -tenant $TenantFilter -message "Verified domain $DomainName" -Sev 'Info' |
| 41 | + $StatusCode = [HttpStatusCode]::OK |
| 42 | + } |
| 43 | + 'delete' { |
| 44 | + Write-Information "Deleting domain $DomainName from tenant $TenantFilter" |
| 45 | + |
| 46 | + $null = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/domains/$DomainName" -tenantid $TenantFilter -type DELETE -AsApp $true |
| 47 | + |
| 48 | + $Result = @{ |
| 49 | + resultText = "Domain $DomainName has been deleted successfully." |
| 50 | + state = 'success' |
| 51 | + } |
| 52 | + |
| 53 | + Write-LogMessage -headers $Request.Headers -API $APIName -tenant $TenantFilter -message "Deleted domain $DomainName" -Sev 'Info' |
| 54 | + $StatusCode = [HttpStatusCode]::OK |
| 55 | + } |
| 56 | + 'setDefault' { |
| 57 | + Write-Information "Setting domain $DomainName as default for tenant $TenantFilter" |
| 58 | + |
| 59 | + $Body = @{ |
| 60 | + isDefault = $true |
| 61 | + } | ConvertTo-Json -Compress |
| 62 | + |
| 63 | + $GraphRequest = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/domains/$DomainName" -tenantid $TenantFilter -type PATCH -body $Body -AsApp $true |
| 64 | + |
| 65 | + $Result = @{ |
| 66 | + resultText = "Domain $DomainName has been set as the default domain successfully." |
| 67 | + state = 'success' |
| 68 | + } |
| 69 | + |
| 70 | + Write-LogMessage -headers $Request.Headers -API $APIName -tenant $TenantFilter -message "Set domain $DomainName as default" -Sev 'Info' |
| 71 | + $StatusCode = [HttpStatusCode]::OK |
| 72 | + } |
| 73 | + default { |
| 74 | + throw "Invalid action: $Action" |
| 75 | + } |
| 76 | + } |
| 77 | + } catch { |
| 78 | + $ErrorMessage = Get-CippException -Exception $_ |
| 79 | + $Result = @{ |
| 80 | + resultText = "Failed to perform action on domain $DomainName`: $($ErrorMessage.NormalizedError)" |
| 81 | + state = 'error' |
| 82 | + } |
| 83 | + Write-LogMessage -headers $Request.Headers -API $APIName -tenant $TenantFilter -message "Failed to perform action on domain $DomainName`: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage |
| 84 | + $StatusCode = [HttpStatusCode]::Forbidden |
| 85 | + } |
| 86 | + |
| 87 | + return ([HttpResponseContext]@{ |
| 88 | + StatusCode = $StatusCode |
| 89 | + Body = @{'Results' = $Result } |
| 90 | + }) |
| 91 | +} |
0 commit comments