Skip to content

Commit c8e3669

Browse files
committed
Add Set-CIPPDeviceState function and refactor Invoke-ExecDeviceDelete to use new function. GET support is maintained
1 parent f94ce24 commit c8e3669

File tree

2 files changed

+93
-15
lines changed

2 files changed

+93
-15
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Identity/Administration/Devices/Invoke-ExecDeviceDelete.ps1

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,28 @@ Function Invoke-ExecDeviceDelete {
1111
param($Request, $TriggerMetadata)
1212

1313
$APIName = $TriggerMetadata.FunctionName
14-
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Accessed this API' -Sev 'Debug'
15-
16-
# Interact with query parameters or the body of the request.
14+
$ExecutingUser = $Request.headers.'x-ms-client-principal'
15+
$TenantFilter = $Request.body.tenantFilter
16+
Write-LogMessage -user $ExecutingUser -API $APINAME -message 'Accessed this API' -Sev 'Debug'
1717

18+
# Interact with body parameters or the body of the request.
19+
$Action = $Request.body.action ?? $Request.Query.action
20+
$DeviceID = $Request.body.ID ?? $Request.Query.ID
1821

1922
try {
20-
$url = "https://graph.microsoft.com/beta/devices/$($request.query.id)"
21-
if ($Request.query.action -eq 'delete') {
22-
$ActionResult = New-GraphPOSTRequest -uri $url -type DELETE -tenantid $Request.Query.TenantFilter
23-
} elseif ($Request.query.action -eq 'disable') {
24-
$ActionResult = New-GraphPOSTRequest -uri $url -type PATCH -tenantid $Request.Query.TenantFilter -body '{"accountEnabled": false }'
25-
} elseif ($Request.query.action -eq 'enable') {
26-
$ActionResult = New-GraphPOSTRequest -uri $url -type PATCH -tenantid $Request.Query.TenantFilter -body '{"accountEnabled": true }'
27-
}
28-
Write-Host $ActionResult
29-
$body = [pscustomobject]@{'Results' = "Executed action $($Request.query.action) on $($Request.query.id)" }
23+
$Results = Set-CIPPDeviceState -Action $Action -DeviceID $DeviceID -TenantFilter $TenantFilter -ExecutingUser $ExecutingUser -APIName $APINAME
24+
$StatusCode = [HttpStatusCode]::OK
3025
} catch {
31-
$body = [pscustomobject]@{'Results' = "Failed to queue action $($Request.query.action) on $($request.query.id): $($_.Exception.Message)" }
26+
$Results = $_.Exception.Message
27+
$StatusCode = [HttpStatusCode]::BadRequest
3228
}
3329

30+
Write-Host $Results
31+
$body = [pscustomobject]@{'Results' = "$Results" }
32+
3433
# Associate values to output bindings by calling 'Push-OutputBinding'.
3534
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
36-
StatusCode = [HttpStatusCode]::OK
35+
StatusCode = $StatusCode
3736
Body = $body
3837
})
3938

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
function Set-CIPPDeviceState {
2+
<#
3+
.SYNOPSIS
4+
Sets or modifies the state of a device in Microsoft Graph.
5+
6+
.DESCRIPTION
7+
This function allows you to enable, disable, or delete a device by making
8+
corresponding requests to the Microsoft Graph API. It logs the result
9+
and returns a success or error message based on the outcome.
10+
11+
.PARAMETER Action
12+
Specifies the action to perform on the device. Valid actions are:
13+
- Enable: Enable the device
14+
- Disable: Disable the device
15+
- Delete: Remove the device from the tenant
16+
17+
.PARAMETER DeviceID
18+
Specifies the unique identifier (Object ID) of the device to be managed.
19+
20+
.PARAMETER TenantFilter
21+
Specifies the tenant ID or domain against which to perform the operation.
22+
23+
.PARAMETER ExecutingUser
24+
Specifies the user who initiated the request for logging purposes.
25+
26+
.PARAMETER APIName
27+
Specifies the name of the API call for logging purposes. Defaults to 'Set Device State'.
28+
29+
.EXAMPLE
30+
Set-CIPPDeviceState -Action Enable -DeviceID "1234abcd-5678-efgh-ijkl-9012mnopqrst" -TenantFilter "contoso.onmicrosoft.com" -ExecutingUser "[email protected]"
31+
32+
This command enables the specified device within the given tenant.
33+
34+
.EXAMPLE
35+
Set-CIPPDeviceState -Action Delete -DeviceID "1234abcd-5678-efgh-ijkl-9012mnopqrst" -TenantFilter "contoso.onmicrosoft.com"
36+
37+
This command removes the specified device from the tenant.
38+
#>
39+
param (
40+
[Parameter(Mandatory = $true)][ValidateSet('Enable', 'Disable', 'Delete')]$Action,
41+
42+
[ValidateScript({
43+
if ([Guid]::TryParse($_, [ref] [Guid]::Empty)) {
44+
$true
45+
} else {
46+
throw 'DeviceID must be a valid GUID.'
47+
}
48+
})]
49+
[Parameter(Mandatory = $true)]$DeviceID,
50+
51+
[Parameter(Mandatory = $true)]$TenantFilter,
52+
$ExecutingUser,
53+
$APIName = 'Set Device State'
54+
)
55+
$Url = "https://graph.microsoft.com/beta/devices/$($DeviceID)"
56+
57+
try {
58+
switch ($Action) {
59+
'Delete' {
60+
$ActionResult = New-GraphPOSTRequest -uri $Url -type DELETE -tenantid $TenantFilter
61+
}
62+
'Disable' {
63+
$ActionResult = New-GraphPOSTRequest -uri $Url -type PATCH -tenantid $TenantFilter -body '{"accountEnabled": false }'
64+
}
65+
'Enable' {
66+
$ActionResult = New-GraphPOSTRequest -uri $Url -type PATCH -tenantid $TenantFilter -body '{"accountEnabled": true }'
67+
}
68+
}
69+
Write-Host $ActionResult
70+
Write-LogMessage -user $ExecutingUser -API $APIName -message "Executed action $($Action) on $($DeviceID)" -Sev Info
71+
return "Executed action $($Action) on $($DeviceID)"
72+
} catch {
73+
$ErrorMessage = Get-CippException -Exception $_
74+
Write-LogMessage -user $ExecutingUser -API $APIName -message "Failed to queue action $($Action) on $($DeviceID). Error: $($ErrorMessage.NormalizedError)" -Sev Error -LogData $ErrorMessage
75+
throw "Failed to queue action $($Action) on $($DeviceID). Error: $($ErrorMessage.NormalizedError)"
76+
}
77+
78+
79+
}

0 commit comments

Comments
 (0)