Skip to content

Commit

Permalink
Merge pull request KelvinTegelaar#465 from johnduprey/dev
Browse files Browse the repository at this point in the history
Mailbox Restores
  • Loading branch information
KelvinTegelaar authored Nov 3, 2023
2 parents 1e78706 + 1fcca38 commit 9902540
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 9 deletions.
18 changes: 18 additions & 0 deletions ExecMailboxRestore/function.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
18 changes: 18 additions & 0 deletions ListMailboxRestores/function.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
29 changes: 20 additions & 9 deletions ListMailboxes/run.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions Modules/CIPPCore/Public/Entrypoints/Invoke-ExecMailboxRestore.ps1
Original file line number Diff line number Diff line change
@@ -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
})
}
43 changes: 43 additions & 0 deletions Modules/CIPPCore/Public/Entrypoints/Invoke-ListMailboxRestores.ps1
Original file line number Diff line number Diff line change
@@ -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)
})
}

0 comments on commit 9902540

Please sign in to comment.