|
| 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