Skip to content

Commit 3b8bcc2

Browse files
committed
Add Invoke-RemoveDeletedObject function for permanently deleting directory items
1 parent 7b2d292 commit 3b8bcc2

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using namespace System.Net
2+
3+
Function Invoke-RemoveDeletedObject {
4+
<#
5+
.FUNCTIONALITY
6+
Entrypoint
7+
.ROLE
8+
Tenant.Directory.ReadWrite
9+
#>
10+
[CmdletBinding()]
11+
param($Request, $TriggerMetadata)
12+
13+
$APIName = $Request.Params.CIPPEndpoint
14+
$Headers = $Request.Headers
15+
Write-LogMessage -headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug'
16+
17+
# Interact with query parameters or the body of the request.
18+
$TenantFilter = $Request.Query.tenantFilter ?? $Request.Body.tenantFilter
19+
$RequestID = $Request.Query.ID ?? $Request.Body.ID
20+
$UserPrincipalName = $Request.Body.userPrincipalName
21+
$DisplayName = $Request.Body.displayName
22+
23+
try {
24+
$null = New-GraphPostRequest -uri "https://graph.microsoft.com/v1.0/directory/deletedItems/$($RequestID)" -tenantid $TenantFilter -type DELETE -body '{}' -Verbose
25+
$Result = "Successfully permanently deleted item with ID: '$($RequestID)'"
26+
if ($UserPrincipalName) { $Result += " User Principal Name: '$($UserPrincipalName)'" }
27+
if ($DisplayName) { $Result += " Display Name: '$($DisplayName)'" }
28+
29+
Write-LogMessage -headers $Headers -tenant $TenantFilter -API $APIName -message $Result -Sev 'Info'
30+
$StatusCode = [HttpStatusCode]::OK
31+
} catch {
32+
$ErrorMessage = Get-CippException -Exception $_
33+
$Result = "Failed to permanently delete item with ID: $($RequestID). Error: $($ErrorMessage.NormalizedError)"
34+
if ($UserPrincipalName) { $Result += " User Principal Name: '$($UserPrincipalName)'" }
35+
if ($DisplayName) { $Result += " Display Name: '$($DisplayName)'" }
36+
37+
Write-LogMessage -headers $Headers -tenant $TenantFilter -API $APIName -message $Result -Sev 'Error' -LogData $ErrorMessage
38+
$StatusCode = [HttpStatusCode]::InternalServerError
39+
}
40+
41+
$Results = [pscustomobject]@{'Results' = $Result }
42+
# Associate values to output bindings by calling 'Push-OutputBinding'.
43+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
44+
StatusCode = $StatusCode
45+
Body = $Results
46+
})
47+
48+
}

0 commit comments

Comments
 (0)