Set-PsIntuneDeviceCategory.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
function Set-PSIntuneDeviceCategory {
    <#
    .SYNOPSIS
        Batch update Intune device category assignment
    .DESCRIPTION
        Batch update Intune device category assignment using
        an input file (csv or txt) to filter the computer names
    .PARAMETER InputFile
        Path and name of .CSV or .TXT input file
        CSV file must have a column named "Computer"
        TXT file must be computer names only, one per line
    .PARAMETER ComputerName
        Computer name(s) to process when InputFile is not used. Comma-delimited string)
    .PARAMETER CategoryName
        A valid category name. If the name does not exist in the
        Intune subscription, it will return an error
    .PARAMETER UserName
        UPN for user with credentials
    .EXAMPLE
        Set-PSIntuneDeviceCategory -InputFile ".\computers.txt" -CategoryName "Personal" -UserName "jdoe@contoso.com"
    .EXAMPLE
        Set-PSIntuneDeviceCategory -ComputerName "Computer1,Computer2" -CategoryName "Corporate" -UserName "jdoe@contoso.com"
    .NOTES
        Requires an Azure AD app registration with Application permissions assigned:
        Device.Read / Read.All / ReadWrite.All
        DeviceManagementManagedDevice.Read.All / ReadWrite.All
        0.1 - 2021-02-03 - David Stein, Catapult Systems
    .LINK
        https://github.com/Skatterbrainz/psIntune/blob/master/docs/Set-PSIntuneDeviceCategory.md
    #>

    [CmdletBinding(SupportsShouldProcess=$True,DefaultParameterSetName='ComputerName')]
    param (
        [parameter(ParameterSetName = 'File', Mandatory=$False)][string] $InputFile = "",
        [parameter(ParameterSetName = 'ComputerName', Mandatory=$False)][string] $ComputerName = "",
        [parameter(Mandatory)][string] $CategoryName,
        [parameter(Mandatory)][string] $UserName
    )
    try {
        if ([string]::IsNullOrEmpty($InputFile) -and [string]::IsNullOrEmpty($ComputerName)) {
            throw "Neither InputFile or ComputerName were provided."
        }
        if (Get-Module AzureAD) {
            Remove-Module AzureAD -Force
        }
        Import-Module AzureADPreview
        Get-psIntuneAuth -UserName $UserName
        if (![string]::IsNullOrEmpty($InputFile)) {
            if (-not(Test-Path $InputFile)) {
                throw "file not found: $InputFile"
            }
            if ($InputFile.EndsWith(".csv")) {
                [array]$computers = Import-Csv -Path $InputFile | Select-Object Computer
            } elseif ($InputFile.EndsWith(".txt")) {
                [array]$computers = Get-Content -Path $InputFile | Where-Object {-not($($_).ToString().StartsWith(';'))}
            } else {
                throw "invalid file type (.csv or .txt only)"
            }
        } else {
            $computers = @($ComputerName -split ',')
        }

        if ($computers.Count -gt 0) {
            Write-Host "processing $($computers.Count) computer names"
        } else {
            throw "no computers were imported from file"
        }

# Write-Verbose "connecting to: azure ad"
# $azconn = Connect-AzureAD -Credential $Credential -ErrorAction Stop
# if (!$WhatIfPreference) { Write-Verbose "connected to $($azconn.TenantDomain)" }

        Write-Verbose "connecting to: msgraph"
        Connect-MSGraph

        Update-MSGraphEnvironment -SchemaVersion 'beta' | Out-Null
        [string]$baseUrl = "https://graph.microsoft.com/beta"

        Write-Verbose "getting devices"
        [array]$devices = Get-PsIntuneDevice -UserName $UserName -Detail Summary
        #[array]$devices = Get-DeviceManagement_ManagedDevices
        Write-Verbose "returned $($devices.Count) devices"

        Write-Verbose "getting device categories"
        [array]$cats = Get-DeviceManagement_DeviceCategories
        Write-Verbose "returned $($cats.Count) categories"

        Write-Verbose "validating requested category: $CategoryName"
        $cat = $cats | Where-Object {$_.displayName -eq $CategoryName}
        if ($null -eq $cat) { throw "category not found: $CategoryName" }
        $DeviceCategory = $cat.id
        Write-Verbose "categoryId........ $DeviceCategory"

        foreach ($computer in $computers) {
            $device = $devices | Where-Object {$_.DeviceName -eq $computer}
            if ($null -eq $device) {
                Write-Warning "device not found: $computer"
            } else {
                $deviceid = $device.DeviceID
                Write-Verbose "deviceName........ $computer"
                Write-Verbose "deviceId.......... $deviceId"
                Write-Verbose "current-category.. $($device.deviceCategoryDisplayName)"
                Set-DeviceCategory -DeviceID $deviceid -category $DeviceCategory -BaseUrl $baseUrl
            }
        }
    }
    catch {
        Write-Error $_.Exception.Message
    }
}