Get device owner from Azure Active Directory group members

Viewed 220

Is there a simple way to get the owners of all devices that are assigned to a particular group? I have a Azure AD group that has devices assigned to it and I would like to change all of the assignments from device to user. Is there a way I can find out the owner of the device and assign them as a member in bulk?

1 Answers

You can use the below Powershell Script to get the the device owners of the devices present in the group.

Connect-AzureAD
$Result=@()
$Members = Get-AzureADGroupMember -ObjectId "b446d49b-****-****-****-************"
foreach ($Member in $Members) {
    $DeviceOwner = $Member|Get-AzureADDeviceRegisteredOwner
    $deviceprops = [ordered] @{
        DeviceDisplayName = $Member.DisplayName
        DeviceObjectID = $Member.ObjectId
        OwnerDisplayName = $DeviceOwner.DisplayName
        OwnerUserPrincipalName = $DeviceOwner.UserPrincipalName
        OwnerObjectID = $DeviceOwner.ObjectId 
    }
    $deviceobj = new-object -Type PSObject -Property $deviceprops
    $Result += $deviceobj
}
$Result 
$Result | Export-CSV "C:\DeviceOwners.csv" 

I have tested the above on my environment I have only devices present in a group.

enter image description here

Output:

enter image description here

Then to Add owners to the group you can import the csv and add the owners to the Group and then similarly remove the Devices from Group. I tested it only for GetADUser as I don’t want to do the changes in my AAD group.

Code:

$Owners = Import-Csv -Path "C:\DeviceOwners.csv"
#$Owners|Format-Table
Connect-AzureAD
$Owners | ForEach-Object {
#$Owner = $_
#Get-AzureADUser -ObjectId $Owner.OwnerObjectID
$Owner = $_
Add-AzureADGroupMember -ObjectId "b446d49b-****-****-****-************" -RefObjectId $Owner.OwnerObjectID
#$Owner = $_
#Remove-AzureADGroupMember -ObjectId "b446d49b-****-****-****-************" -MemberId $Owner.DeviceObjectID
}

Note : First Add the Owners to the Group . While running the above script to add Owners to Group keep the Remove command commented . After you have added the Owners to the Group then you can comment the Add command and use the Remove command to remove the Devices from the Group.

enter image description here

Reference:

Install Powersell azuread module : Command: Install-Module -Name AzureAD

AzureAD Group Module | Microsoft Docs

Related