Skip to content

Commit eb9bcaa

Browse files
committed
Refactor Invoke-ExecDeviceAction and New-CIPPDeviceAction to improve logging and add support for changing primary user
1 parent 1efed26 commit eb9bcaa

File tree

2 files changed

+45
-29
lines changed

2 files changed

+45
-29
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Endpoint/MEM/Invoke-ExecDeviceAction.ps1

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,45 @@ Function Invoke-ExecDeviceAction {
1515
Write-LogMessage -headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug'
1616

1717
# Interact with Body parameters or the body of the request.
18-
18+
$Action = $Request.Body.Action
19+
$DeviceFilter = $Request.Body.GUID
20+
$TenantFilter = $Request.Body.tenantFilter
1921

2022
try {
21-
if ($Request.Body.Action -eq 'setDeviceName') {
22-
$ActionBody = @{ deviceName = $Request.Body.input } | ConvertTo-Json -Compress
23-
}
24-
else {
25-
$ActionBody = $Request.Body | ConvertTo-Json -Compress
23+
switch ($Action) {
24+
'setDeviceName' {
25+
$ActionBody = @{ deviceName = $Request.Body.input } | ConvertTo-Json -Compress
26+
break
27+
}
28+
'users' {
29+
$ActionBody = @{ '@odata.id' = "https://graph.microsoft.com/beta/users('$($Request.Body.user.value)')" } | ConvertTo-Json -Compress
30+
Write-Host "ActionBody: $ActionBody"
31+
break
32+
}
33+
Default { $ActionBody = $Request.Body | ConvertTo-Json -Compress }
2634
}
2735

28-
$cmdparams = @{
29-
Action = $Request.Body.Action
30-
ActionBody = $ActionBody
31-
DeviceFilter = $Request.Body.GUID
32-
TenantFilter = $Request.Body.TenantFilter
33-
Headers = $Request.Headers
34-
APINAME = $APINAME
36+
$cmdParams = @{
37+
Action = $Action
38+
ActionBody = $ActionBody
39+
DeviceFilter = $DeviceFilter
40+
TenantFilter = $TenantFilter
41+
Headers = $Headers
42+
APINAME = $APIName
3543
}
36-
$ActionResult = New-CIPPDeviceAction @cmdparams
44+
$ActionResult = New-CIPPDeviceAction @cmdParams
3745

38-
$body = [pscustomobject]@{'Results' = "$ActionResult" }
46+
$StatusCode = [HttpStatusCode]::OK
47+
$Results = "$ActionResult"
3948

4049
} catch {
41-
$body = [pscustomobject]@{'Results' = "Failed to queue action $action on $DeviceFilter $($_.Exception.Message)" }
50+
$StatusCode = [HttpStatusCode]::InternalServerError
51+
$Results = "$($_.Exception.Message)"
4252
}
4353

4454
# Associate values to output bindings by calling 'Push-OutputBinding'.
4555
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
46-
StatusCode = [HttpStatusCode]::OK
47-
Body = $body
56+
StatusCode = $StatusCode
57+
Body = @{ 'Results' = $Results }
4858
})
49-
5059
}

Modules/CIPPCore/Public/New-CIPPDeviceAction.ps1

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,27 @@ function New-CIPPDeviceAction {
66
$DeviceFilter,
77
$TenantFilter,
88
$Headers,
9-
$APINAME
9+
$APIName
1010
)
1111
try {
12-
if ($action -eq 'delete') {
13-
$null = New-Graphpostrequest -uri "https://graph.microsoft.com/beta/deviceManagement/managedDevices/$DeviceFilter" -type DELETE -tenantid $TenantFilter
14-
Write-LogMessage -headers $Headers -API $APINAME -tenant $TenantFilter -message "Queued $Action on $DeviceFilter" -Sev 'Info'
15-
return "Queued $Action on $DeviceFilter"
12+
if ($Action -eq 'delete') {
13+
$null = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/managedDevices/$DeviceFilter" -type DELETE -tenantid $TenantFilter
14+
} elseif ($Action -eq 'users') {
15+
$null = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/managedDevices('$DeviceFilter')/$($Action)/`$ref" -type POST -tenantid $TenantFilter -body $ActionBody
16+
$regex = "(?<=\(')([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})(?='|\))"
17+
$PrimaryUser = $ActionBody | Select-String -Pattern $regex -AllMatches | Select-Object -ExpandProperty Matches | Select-Object -ExpandProperty Value
18+
$Result = "Changed primary user on device $DeviceFilter to $PrimaryUser"
19+
} else {
20+
$null = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/managedDevices('$DeviceFilter')/$($Action)" -type POST -tenantid $TenantFilter -body $ActionBody
21+
$Result = "Queued $Action on $DeviceFilter"
1622
}
17-
$null = New-Graphpostrequest -uri "https://graph.microsoft.com/beta/deviceManagement/managedDevices('$DeviceFilter')/$($Action)" -type POST -tenantid $TenantFilter -body $ActionBody
18-
Write-LogMessage -headers $Headers -API $APINAME -tenant $TenantFilter -message "Queued $Action on $DeviceFilter" -Sev 'Info'
19-
return "Queued $Action on $DeviceFilter"
23+
24+
Write-LogMessage -headers $Headers -API $APIName -tenant $TenantFilter -message $Result -Sev Info
25+
return $Result
2026
} catch {
2127
$ErrorMessage = Get-CippException -Exception $_
22-
Write-LogMessage -headers $Headers -API $APINAME -tenant $TenantFilter -message "Failed to queue action $Action on $DeviceFilter : $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
23-
return "Failed to queue action $Action on $DeviceFilter $($ErrorMessage.NormalizedError)"
28+
$Result = "Failed to queue action $Action on $DeviceFilter : $($ErrorMessage.NormalizedError)"
29+
Write-LogMessage -headers $Headers -API $APIName -tenant $TenantFilter -message $Result -Sev Error -LogData $ErrorMessage
30+
throw $Result
2431
}
2532
}

0 commit comments

Comments
 (0)