Skip to content

Instantly share code, notes, and snippets.

@jpawlowski
Last active June 11, 2024 09:15
Show Gist options
  • Save jpawlowski/d9e7cb61a36a4a22a69a935657e77335 to your computer and use it in GitHub Desktop.
Save jpawlowski/d9e7cb61a36a4a22a69a935657e77335 to your computer and use it in GitHub Desktop.
Set extension attribute for a device.
<#PSScriptInfo
.VERSION 1.0.0
.GUID 380c73f8-ef67-4aeb-ae6b-2f5e563fabb8
.AUTHOR Julian Pawlowski
.COMPANYNAME Julian Pawlowski
.COPYRIGHT © 2024 Julian Pawlowski
.TAGS
.LICENSEURI https://opensource.org/license/MIT
.PROJECTURI https://gist.github.com/jpawlowski/d9e7cb61a36a4a22a69a935657e77335
.ICONURI
.EXTERNALMODULEDEPENDENCIES 'Microsoft.Graph.Authentication'
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
Version 1.0.0 (2024-06-11)
- Initial release.
#>
<#
.SYNOPSIS
Set extension attribute for a device.
.DESCRIPTION
Set extension attribute for a device.
.PARAMETER DeviceId
The ID or display name of the device.
.PARAMETER ExtensionAttribute
The extension attribute number (1-15).
.PARAMETER Value
The value to set.
#>
#Requires -Modules @{ ModuleName = 'Microsoft.Graph.Authentication'; ModuleVersion = '2.0.0'}
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$DeviceId,
[Parameter(Mandatory = $true)]
[ValidateRange(1, 15)]
[int]$ExtensionAttribute,
[Parameter(Mandatory = $true)]
[string]$Value
)
if (-not (Get-MgContext)) {
$params = @{
Scopes = @(
'Device.ReadWrite.All'
)
ContextScope = 'Process'
NoWelcome = $true
ErrorAction = 'Stop'
}
Connect-MgGraph @params
}
if ($DeviceId -notmatch '^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$') {
$DeviceId = (Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/v1.0/devices?`$filter=displayName eq '$DeviceId'" -ErrorAction 'Stop').value.id
if (-not $DeviceId) {
Write-Error 'Device not found' -ErrorAction 'Stop'
exit 1
}
}
$params = @{
Uri = "https://graph.microsoft.com/v1.0/devices/$DeviceId"
Method = 'PATCH'
Body = @{
extensionAttributes = @{
"extensionAttribute$ExtensionAttribute" = $Value
}
}
ErrorAction = 'Stop'
}
Invoke-MgGraphRequest @params
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment