Public/Invoke-DeviceAction.ps1

<#
 
.COPYRIGHT
Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
See https://github.com/microsoftgraph/powershell-intune-samples/blob/master/LICENSE for license information.
 
#>


Function Invoke-DeviceAction() {
    
        <#
    .SYNOPSIS
    This function is used to set a generic intune resources from the Graph API REST interface
    .DESCRIPTION
    The function connects to the Graph API Interface and sets a generic Intune Resource
    .EXAMPLE
    Invoke-DeviceAction -DeviceID $DeviceID -remoteLock
    Resets a managed device passcode
    .NOTES
    NAME: Invoke-DeviceAction
    #>

    
        [cmdletbinding()]
    
        param
        (
            [switch]$RemoteLock,
            [switch]$ResetPasscode,
            [switch]$Wipe,
            [switch]$Retire,
            [switch]$Delete,
            [switch]$Sync,
            [Parameter(Mandatory = $true, HelpMessage = "DeviceId (guid) for the Device you want to take action on must be specified:")]
            $DeviceID
        )
    
        $graphApiVersion = "Beta"
    
        try {
    
            $Count_Params = 0
    
            if ($RemoteLock.IsPresent) { $Count_Params++ }
            if ($ResetPasscode.IsPresent) { $Count_Params++ }
            if ($Wipe.IsPresent) { $Count_Params++ }
            if ($Retire.IsPresent) { $Count_Params++ }
            if ($Delete.IsPresent) { $Count_Params++ }
            if ($Sync.IsPresent) { $Count_Params++ }
    
            if ($Count_Params -eq 0) {
    
                write-host "No parameter set, specify -RemoteLock -ResetPasscode -Wipe -Delete or -Sync against the function" -f Red
    
            }
    
            elseif ($Count_Params -gt 1) {
    
                write-host "Multiple parameters set, specify a single parameter -RemoteLock -ResetPasscode -Wipe -Delete or -Sync against the function" -f Red
    
            }
    
            elseif ($RemoteLock) {
    
                $Resource = "managedDevices/$DeviceID/remoteLock"
                $uri = "https://graph.microsoft.com/$graphApiVersion/$($resource)"
                write-verbose $uri
                Write-Verbose "Sending remoteLock command to $DeviceID"
                Invoke-RestMethod -Uri $uri -Headers $authToken -Method Post
    
            }
    
            elseif ($ResetPasscode) {
    
                            
                write-host "Are you sure you want to reset the Passcode this device? Y or N?"
                $Confirm = read-host
    
                if ($Confirm -eq "y" -or $Confirm -eq "Y") {
    
                    $Resource = "managedDevices/$DeviceID/resetPasscode"
                    $uri = "https://graph.microsoft.com/$graphApiVersion/$($resource)"
                    write-verbose $uri
                    Write-Verbose "Sending remotePasscode command to $DeviceID"
                    Invoke-RestMethod -Uri $uri -Headers $authToken -Method Post
    
                }
    
                else {
    
                    Write-Host "Reset of the Passcode for the device $DeviceID was cancelled..."
    
                }
    
            }
    
            elseif ($Wipe) {
    
                    
                write-host "Are you sure you want to wipe this device? Y or N?"
                $Confirm = read-host
    
                if ($Confirm -eq "y" -or $Confirm -eq "Y") {
    
                    $Resource = "managedDevices/$DeviceID/wipe"
                    $uri = "https://graph.microsoft.com/$graphApiVersion/$($resource)"
                    write-verbose $uri
                    Write-Verbose "Sending wipe command to $DeviceID"
                    Invoke-RestMethod -Uri $uri -Headers $authToken -Method Post
    
                }
    
                else {
    
                    Write-Host "Wipe of the device $DeviceID was cancelled..."
    
                }
    
            }
    
            elseif ($Retire) {
    
                    
                write-host "Are you sure you want to retire this device? Y or N?"
                $Confirm = read-host
    
                if ($Confirm -eq "y" -or $Confirm -eq "Y") {
    
                    $Resource = "managedDevices/$DeviceID/retire"
                    $uri = "https://graph.microsoft.com/$graphApiVersion/$($resource)"
                    write-verbose $uri
                    Write-Verbose "Sending retire command to $DeviceID"
                    Invoke-RestMethod -Uri $uri -Headers $authToken -Method Post
    
                }
    
                else {
    
                    Write-Host "Retire of the device $DeviceID was cancelled..."
    
                }
    
            }
    
            elseif ($Delete) {
    
                    
                Write-Warning "A deletion of a device will only work if the device has already had a retire or wipe request sent to the device..."
                    
                write-host "Are you sure you want to delete this device? Y or N?"
                $Confirm = read-host
    
                if ($Confirm -eq "y" -or $Confirm -eq "Y") {
    
                    $Resource = "managedDevices('$DeviceID')"
                    $uri = "https://graph.microsoft.com/$graphApiVersion/$($resource)"
                    write-verbose $uri
                    Write-Verbose "Sending delete command to $DeviceID"
                    Invoke-RestMethod -Uri $uri -Headers $authToken -Method Delete
    
                }
    
                else {
    
                    Write-Host "Deletion of the device $DeviceID was cancelled..."
    
                }
    
            }
                    
            elseif ($Sync) {
    
                    
                write-host "Are you sure you want to sync this device? Y or N?"
                $Confirm = read-host
    
                if ($Confirm -eq "y" -or $Confirm -eq "Y") {
    
                    $Resource = "managedDevices('$DeviceID')/syncDevice"
                    $uri = "https://graph.microsoft.com/$graphApiVersion/$($resource)"
                    write-verbose $uri
                    Write-Verbose "Sending sync command to $DeviceID"
                    Invoke-RestMethod -Uri $uri -Headers $authToken -Method Post
    
                }
    
                else {
    
                    Write-Host "Sync of the device $DeviceID was cancelled..."
    
                }
    
            }
    
        }
    
        catch {
    
            $ex = $_.Exception
            $errorResponse = $ex.Response.GetResponseStream()
            $reader = New-Object System.IO.StreamReader($errorResponse)
            $reader.BaseStream.Position = 0
            $reader.DiscardBufferedData()
            $responseBody = $reader.ReadToEnd();
            Write-Host "Response content:`n$responseBody" -f Red
            Write-Error "Request to $Uri failed with HTTP Status $($ex.Response.StatusCode) $($ex.Response.StatusDescription)"
            
            break
    
        }
    
    }