For line 3 please take a look at a way at calling Import-csv for a list of device IDs so we don’t deploy to all, Script below is looking for a device and matching it against the owner info then writing the ExAttrib from the user account to the device ExAttrib in AAD.<\/p>\n
Connect-MgGraph -Scopes “Directory.AccessAsUser.All”
\nSelect-MgProfile Beta
\n##[array]$Devices = Get-MgDevice -All ### Need to change to list of Device ID from CSV for testing<\/p>\n
ForEach ($Device in $Devices) { } | ConvertTo-Json For line 3 please take a look at a way at calling Import-csv for a list of device IDs so we don’t deploy to all, Script below is looking for a device and matching it against the owner info then writing the ExAttrib from the user account to the device ExAttrib in AAD.<\/p>\n Connect-MgGraph -Scopes “Directory.AccessAsUser.All” ForEach ($Device in $Devices) { } | ConvertTo-Json When inserting code, please use the “Insert Code” button </>. It makes it a lot easier to read.<\/p>\n Finding the device is dependent on what’s in your CSV and how it’s formatted. Here’s a generic idea<\/p>\n I’d recommend using the Filter option if possible. The documentation<\/a> has a number of samples on finding devices.<\/p>","upvoteCount":1,"datePublished":"2022-10-26T10:33:45.000Z","url":"https://community.spiceworks.com/t/review-the-below-powershell-script/939042/2","author":{"@type":"Person","name":"saidbrandon","url":"https://community.spiceworks.com/u/saidbrandon"}},{"@type":"Answer","text":"
\nIf ($Device.PhysicalIds.count -gt 0) {
\nForeach ($X in $Device.PhysicalIds) { If ($X.SubString(0,10) -eq “[USER-GID]”) { $UserGuid = $X } }
\n$UserId = $UserGuid.substring(11,36)
\nIf ($UserId) { #We<\/span> found a user identifier - try to resolve it against Azure AD
\n[array]$User = Get-MgUser -UserId $UserId -ErrorAction SilentlyContinue }
\nIf ($User) { # Found a user in Azure AD
\nWrite-Host (“Device {0} owned by {1}” -f $Device.DisplayName, $User.DisplayName)
\n$Attributes = @{
\n“extensionAttributes” = @{
\n“extensionAttribute13” = $User.extensionAttribute13<\/p>\n
\nUpdate-MgDevice -DeviceId $Device.Id -BodyParameter $Attributes
\n}
\nElse { Write-Host (“Device {0} owned by unknown user {1}” -f $Device.DisplayName, $UserId ) }
\n} # End If Device PhysicalsId
\n} #End<\/span> Foreach<\/p>","upvoteCount":7,"answerCount":6,"datePublished":"2022-10-26T09:46:34.000Z","author":{"@type":"Person","name":"akashgowda","url":"https://community.spiceworks.com/u/akashgowda"},"suggestedAnswer":[{"@type":"Answer","text":"
\nSelect-MgProfile Beta
\n##[array]$Devices = Get-MgDevice -All ### Need to change to list of Device ID from CSV for testing<\/p>\n
\nIf ($Device.PhysicalIds.count -gt 0) {
\nForeach ($X in $Device.PhysicalIds) { If ($X.SubString(0,10) -eq “[USER-GID]”) { $UserGuid = $X } }
\n$UserId = $UserGuid.substring(11,36)
\nIf ($UserId) { #We<\/span> found a user identifier - try to resolve it against Azure AD
\n[array]$User = Get-MgUser -UserId $UserId -ErrorAction SilentlyContinue }
\nIf ($User) { # Found a user in Azure AD
\nWrite-Host (“Device {0} owned by {1}” -f $Device.DisplayName, $User.DisplayName)
\n$Attributes = @{
\n“extensionAttributes” = @{
\n“extensionAttribute13” = $User.extensionAttribute13<\/p>\n
\nUpdate-MgDevice -DeviceId $Device.Id -BodyParameter $Attributes
\n}
\nElse { Write-Host (“Device {0} owned by unknown user {1}” -f $Device.DisplayName, $UserId ) }
\n} # End If Device PhysicalsId
\n} #End<\/span> Foreach<\/p>","upvoteCount":7,"datePublished":"2022-10-26T09:46:34.000Z","url":"https://community.spiceworks.com/t/review-the-below-powershell-script/939042/1","author":{"@type":"Person","name":"akashgowda","url":"https://community.spiceworks.com/u/akashgowda"}},{"@type":"Answer","text":"$Devices = Import-Csv -Path \"C:\\Path\\File.csv\"\n\n$Devices | ForEach-Object {\n $Device = Get-MGDevice -DeviceId $_.DEVICEIDCOLUMNNAME -ErrorAction SilentlyContinue\n # OR\n $Device = Get-MGDevice -Filter \"extensionAttributes/extensionAttribute1 eq 'BYOD-Device'\"\n\n if ($null -ne $Device) {\n # DO STUFF\n }\n}\n<\/code><\/pre>\n