cannot find a script to get object ID of devices from a device name list

Nadimuddin J Shaikh 1 Reputation point
2022-06-08T09:45:59.583+00:00

we have a list of 80 devices with device names
We need to upload these to a Azure AD group But for this I need the Object IF of these devices
I need a script or command which will take the device name from the file containing the device and get the ObjectIDs of these devices to another file

Once this is done I can upload that file in the import field of the Azure AD group

Windows for business Windows Server User experience PowerShell
{count} votes

3 answers

Sort by: Most helpful
  1. Newbie Jones 1,386 Reputation points
    2022-06-08T14:55:44.177+00:00

    Get-AzureADDevice and Filter.

    $devices = 'computerA', 'computerB' # or Import-CSV  
    $devices | ForEach {Get-AzureADDevice -Filter "DisplayName eq '$_'"} |   
        Select-Object Displayname, ObjectId  
    

    Yes, that's double and single quotes in the filter.

    You should be able to pipe the results of this directly into the Add-AzureADGroupMember cmdlet.

    1 person found this answer helpful.
    0 comments No comments

  2. Nick Von Ogden 51 Reputation points
    2022-06-10T06:43:20.863+00:00

    https://learn.microsoft.com/en-us/powershell/module/azuread/get-azureaddevice?view=azureadps-2.0 you can use the -SearchString parameter or -Filter. Either way you'll probably want to confirm the uniqueness of the 'name' you are searching. I haven't tested my code below but this should get you started. I made some assumptions on how you were pulling in said data, but you may need to update some stuff. Also if a search returns more than one object, I just added a quick bit of logic to write a warning instead and not add to the AD Group. Modify as you need of course.

    $Devices = Import-Csv -Path "PathtoCSV.csv"  
    foreach ($device in $devices) {  
        $AzureADDevice = Get-AzureADDevice -SearchString "$($Devices.name)"  
        if ($AzureADDevice.count -eq 1) {  
            Add-AzureADGroupMember -ObjectID $ObjectIDOfGroup -RefObjectID $DeviceObject.ObjectID  
        } else {  
            Write-Warning -Message "Multiple devices found with display name $($AzureADDevice[0].displayname)"  
        }  
    }  
      
    
    0 comments No comments

  3. Limitless Technology 39,916 Reputation points
    2022-06-10T07:09:48.557+00:00

    Hi there,

    If you have the device Names and their Azure AD Device IDs, the below script will help you in adding Azure AD joined devices to an AAD group.

    $groupName = "myAADGroupName"
    try {
    $deviceList = Import-Csv -Path "D:\DevicesToAdd.csv"
    Connect-AzureAD
    $groupObj = Get-AzureADGroup -SearchString $groupName
    foreach ($device in $deviceList) {
    $deviceObj = Get-AzureADDevice -SearchString $device.DeviceName
    if($deviceObj -ne $null){
    try{
    foreach($dev in $deviceObj){
    if($dev.DeviceId -eq $device.azureADDeviceId){
    Add-AzureADGroupMember -ObjectId $groupObj.ObjectId -RefObjectId $dev.ObjectId
    }
    }
    }
    catch{}
    }
    else{
    Write-Host "No device found:$($device.DeviceName)"
    }
    }
    }
    catch {
    Write-Host -Message $_
    }

    Hope this resolves your Query !!

    ------------------------------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept it as an answer–

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.