From 4bbd61837d3db9a2216c6214795739290f8717b3 Mon Sep 17 00:00:00 2001 From: John Duprey Date: Fri, 3 Nov 2023 13:49:30 -0400 Subject: [PATCH] Mailbox Restores --- ExecMailboxRestore/function.json | 18 +++++++ ListMailboxRestores/function.json | 18 +++++++ ListMailboxes/run.ps1 | 29 ++++++---- .../Entrypoints/Invoke-ExecMailboxRestore.ps1 | 54 +++++++++++++++++++ .../Invoke-ListMailboxRestores.ps1 | 43 +++++++++++++++ 5 files changed, 153 insertions(+), 9 deletions(-) create mode 100644 ExecMailboxRestore/function.json create mode 100644 ListMailboxRestores/function.json create mode 100644 Modules/CIPPCore/Public/Entrypoints/Invoke-ExecMailboxRestore.ps1 create mode 100644 Modules/CIPPCore/Public/Entrypoints/Invoke-ListMailboxRestores.ps1 diff --git a/ExecMailboxRestore/function.json b/ExecMailboxRestore/function.json new file mode 100644 index 000000000000..bf6c3ef0c49a --- /dev/null +++ b/ExecMailboxRestore/function.json @@ -0,0 +1,18 @@ +{ + "scriptFile": "../Modules/CippEntryPoints/CippEntryPoints.psm1", + "entryPoint": "Receive-CippHttpTrigger", + "bindings": [ + { + "authLevel": "anonymous", + "type": "httpTrigger", + "direction": "in", + "name": "Request", + "methods": ["get", "post"] + }, + { + "type": "http", + "direction": "out", + "name": "Response" + } + ] +} diff --git a/ListMailboxRestores/function.json b/ListMailboxRestores/function.json new file mode 100644 index 000000000000..bf6c3ef0c49a --- /dev/null +++ b/ListMailboxRestores/function.json @@ -0,0 +1,18 @@ +{ + "scriptFile": "../Modules/CippEntryPoints/CippEntryPoints.psm1", + "entryPoint": "Receive-CippHttpTrigger", + "bindings": [ + { + "authLevel": "anonymous", + "type": "httpTrigger", + "direction": "in", + "name": "Request", + "methods": ["get", "post"] + }, + { + "type": "http", + "direction": "out", + "name": "Response" + } + ] +} diff --git a/ListMailboxes/run.ps1 b/ListMailboxes/run.ps1 index c492cf59fcec..8f617d95006b 100644 --- a/ListMailboxes/run.ps1 +++ b/ListMailboxes/run.ps1 @@ -4,33 +4,44 @@ using namespace System.Net param($Request, $TriggerMetadata) $APIName = $TriggerMetadata.FunctionName -Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message "Accessed this API" -Sev "Debug" +Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Accessed this API' -Sev 'Debug' # Write to the Azure Functions log stream. -Write-Host "PowerShell HTTP trigger function processed a request." +Write-Host 'PowerShell HTTP trigger function processed a request.' # Interact with query parameters or the body of the request. $TenantFilter = $Request.Query.TenantFilter try { $users = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/users/?`$top=999&`$select=id,userPrincipalName,assignedLicenses" -Tenantid $tenantfilter - $GraphRequest = (New-ExoRequest -tenantid $TenantFilter -cmdlet "Get-mailbox") | Select-Object id, @{ Name = 'UPN'; Expression = { $_.'UserPrincipalName' } }, + + $ExoRequest = @{ + tenantid = $TenantFilter + cmdlet = 'Get-Mailbox' + } + + if ([bool]$Request.Query.SoftDeletedMailbox -eq $true) { + $ExoRequest.cmdParams = @{ SoftDeletedMailbox = $true } + } + + Write-Host ($ExoRequest | ConvertTo-Json) + + $GraphRequest = (New-ExoRequest @ExoRequest) | Select-Object id, ExchangeGuid, ArchiveGuid, @{ Name = 'UPN'; Expression = { $_.'UserPrincipalName' } }, @{ Name = 'displayName'; Expression = { $_.'DisplayName' } }, - @{ Name = 'SharedMailboxWithLicense'; Expression = { + @{ Name = 'SharedMailboxWithLicense'; Expression = { $ID = $_.id $Shared = if ($_.'RecipientTypeDetails' -eq 'SharedMailbox') { $true } else { $false } - if (($users | Where-Object -Property ID -EQ $ID).assignedLicenses.skuid -and $Shared) { $true } else { $false } - } + if (($users | Where-Object -Property ID -EQ $ID).assignedLicenses.skuid -and $Shared) { $true } else { $false } + } }, @{ Name = 'primarySmtpAddress'; Expression = { $_.'PrimarySMTPAddress' } }, @{ Name = 'recipientType'; Expression = { $_.'RecipientType' } }, @{ Name = 'recipientTypeDetails'; Expression = { $_.'RecipientTypeDetails' } }, - @{ Name = 'AdditionalEmailAddresses'; Expression = { ($_.'EmailAddresses' | Where-Object { $_ -clike 'smtp:*' }).Replace('smtp:', '') -join ", " } } + @{ Name = 'AdditionalEmailAddresses'; Expression = { ($_.'EmailAddresses' | Where-Object { $_ -clike 'smtp:*' }).Replace('smtp:', '') -join ', ' } } $StatusCode = [HttpStatusCode]::OK -} -catch { +} catch { $ErrorMessage = Get-NormalizedError -Message $_.Exception.Message $StatusCode = [HttpStatusCode]::Forbidden $GraphRequest = $ErrorMessage diff --git a/Modules/CIPPCore/Public/Entrypoints/Invoke-ExecMailboxRestore.ps1 b/Modules/CIPPCore/Public/Entrypoints/Invoke-ExecMailboxRestore.ps1 new file mode 100644 index 000000000000..f483d40ae7a7 --- /dev/null +++ b/Modules/CIPPCore/Public/Entrypoints/Invoke-ExecMailboxRestore.ps1 @@ -0,0 +1,54 @@ +function Invoke-ExecMailboxRestore { + Param($Request, $TriggerMetadata) + + $APIName = $TriggerMetadata.FunctionName + Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Accessed this API' -Sev 'Debug' + + # Interact with query parameters or the body of the request. + $TenantFilter = $Request.Body.TenantFilter + $RequestName = $Request.Body.RequestName + $SourceMailbox = $Request.Body.SourceMailbox + $TargetMailbox = $Request.Body.TargetMailbox + + try { + $ExoRequest = @{ + tenantid = $TenantFilter + cmdlet = 'New-MailboxRestoreRequest' + cmdParams = @{ + Name = $RequestName + SourceMailbox = $SourceMailbox + TargetMailbox = $TargetMailbox + AllowLegacyDNMismatch = $true + } + } + if ([bool]$Request.Body.AcceptLargeDataLoss -eq $true) { + $ExoRequest.cmdParams.AcceptLargeDataLoss = $true + } + if ([int]$Request.Body.BadItemLimit -gt 0) { + $ExoRequest.cmdParams.BadItemLimit = $Request.Body.BadItemLimit + } + if ([int]$Request.Body.LargeItemLimit -gt 0) { + $ExoRequest.cmdParams.LargeItemLimit = $Request.Body.LargeItemLimit + } + + $GraphRequest = New-ExoRequest @ExoRequest + + $Body = @{ + RestoreRequest = $GraphRequest + Results = @('Mailbox restore request started successfully') + } + $StatusCode = [HttpStatusCode]::OK + } catch { + $ErrorMessage = Get-NormalizedError -Message $_.Exception.Message + $StatusCode = [HttpStatusCode]::Forbidden + $Body = @{ + RestoreRequest = $null + Results = @($ErrorMessage) + } + } + # Associate values to output bindings by calling 'Push-OutputBinding'. + Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ + StatusCode = $StatusCode + Body = $Body + }) +} \ No newline at end of file diff --git a/Modules/CIPPCore/Public/Entrypoints/Invoke-ListMailboxRestores.ps1 b/Modules/CIPPCore/Public/Entrypoints/Invoke-ListMailboxRestores.ps1 new file mode 100644 index 000000000000..f47458585c09 --- /dev/null +++ b/Modules/CIPPCore/Public/Entrypoints/Invoke-ListMailboxRestores.ps1 @@ -0,0 +1,43 @@ +function Invoke-ListMailboxRestores { + param($Request, $TriggerMetadata) + + $APIName = $TriggerMetadata.FunctionName + Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Accessed this API' -Sev 'Debug' + + # Interact with query parameters or the body of the request. + $TenantFilter = $Request.Query.TenantFilter + try { + if ([bool]$Request.Query.Statistics -eq $true -and $Request.Query.Identity) { + $ExoRequest = @{ + tenantid = $TenantFilter + cmdlet = 'Get-MailboxRestoreRequestStatistics' + cmdParams = @{ Identity = $Request.Query.Identity } + } + + if ([bool]$Request.Query.IncludeReport -eq $true) { + $ExoRequest.cmdParams.IncludeReport = $true + } + $GraphRequest = New-ExoRequest @ExoRequest + + } else { + $ExoRequest = @{ + tenantid = $TenantFilter + cmdlet = 'Get-MailboxRestoreRequest' + } + + $RestoreRequests = (New-ExoRequest @ExoRequest) + $GraphRequest = $RestoreRequests + } + + $StatusCode = [HttpStatusCode]::OK + } catch { + $ErrorMessage = Get-NormalizedError -Message $_.Exception.Message + $StatusCode = [HttpStatusCode]::Forbidden + $GraphRequest = $ErrorMessage + } + # Associate values to output bindings by calling 'Push-OutputBinding'. + Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ + StatusCode = $StatusCode + Body = @($GraphRequest) + }) +}