Forum Discussion

Jaime_IT's avatar
Jaime_IT
Brass Contributor
Dec 21, 2023

Device Bulk Rename Struggle

We've struggled with this for quite some time now.

 

What we need to do is, discover all Intune devices with one of the following elements true:

10.1.x.x IP address
10.111.x.x IP address

Then rename the discovered devices to A-{{serialnumber}}. There should be around 5000 devices matching.

 

To do this, we understand that we need to get the device info from Intune, export to csv, then script a bulk rename process referencing the csv.

 

When we script to get this info and export to csv, it fails to get the leading zeros in serialNumber, gets the UPN in UID format rather than plain text, fails to get ipAddressV4, and returns System.Object[] for wiredIPv4Addresses. Yet all of this info we can clearly see in the Intune console, in plain text, for a single device at a time.

 

In Graph Explorer, the following GET will pull the info needed for a single device:

GET //graph.microsoft.com/beta/deviceManagement/manageddevices('IntuneDeviceId')?$select=devicename,id,hardwareinformation

 

We must be missing something in scripting. Would someone please provide some insight?

 

TIA!

 

~Jaime

  • Another approach would be to run a Detection/Remediation script on the client if the client matches the 10.1* range. If it does, then use the Rename-Computer cmdlet to rename the client, and when the device reboots/shuts, will the name in Intune change? (I've done this in the past and renamed Intune deployed devices to the Asset tag from the bios)

     


    Please click Mark as Best Response & Like if my post helped you to solve your issue.
    This will help others to find the correct solution easily. It also closes the item.

    If one of the posts was helpful in other ways, please consider giving it a Like.

    • Jaime_IT's avatar
      Jaime_IT
      Brass Contributor

      Interesting. Not the Microsoft recommended process, yet it seems completely plausible.

       

      I'll investigate this suggestion and get back to you. Thanks!

      • Let us know! This is what I used to rename the computer based on Asset Tag (replaced value with XXX to avoid mentioning customer name)

         

        if ($env:COMPUTERNAME -notmatch 'XXX') {
        if (((Get-WmiObject win32_SystemEnclosure).smbiosassettag -ne $env:COMPUTERNAME)) {
        Rename-Computer -NewName ((Get-WmiObject win32_SystemEnclosure).smbiosassettag)
        }
        }

         

         

  • Jaime_IT's avatar
    Jaime_IT
    Brass Contributor

    Here is the script so far:

    # Define App Registration details
    $appId = "appId"
    $tenantId = "tenantId"
    $clientSecret = "clientSecret"
    $resource = "https://graph.microsoft.com/"
    
    # Get OAuth token
    $tokenUrl = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
    $body = @{
        grant_type    = "client_credentials"
        client_id     = $appId
        client_secret = $clientSecret
        scope         = "https://graph.microsoft.com/.default"
    }
    
    $Response = Invoke-RestMethod -Uri $tokenUrl -Method Post -Body $Body
    $token = $Response.access_token
    
    # Define the CSV file path
    $csvPath = "H:\IT\Jaime\IntuneDevices_ja4t3.csv"
    
     # Define the API URL to get Intune managed devices
    $apiUrl = "https://graph.microsoft.com/beta/deviceManagement/managedDevices"
    
    # Initialize an array to hold the devices info
    $devicesInfo = @()
    
    # Call the API and filter the devices
    do {
        $response = Invoke-RestMethod -Headers @{Authorization = "Bearer $token"} -Uri $apiUrl
        foreach ($device in $response.value) {
            if ($device.deviceName -like "laptop-*") {
                $hardwareInfo = Invoke-RestMethod -Headers @{Authorization = "Bearer $token"} -Uri "https://graph.microsoft.com/beta/deviceManagement/manageddevices"
                $devicesInfo += [PSCustomObject]@{
                    DeviceName = $device.deviceName
                    SerialNumber = $device.serialNumber.PadLeft(9, '0')
                    User = $device.usersLoggedOn[-1].userId
                    WiFiIPv4 = $device.hardwareInformation.ipAddressV4
                    WiredIPv4 = $device.hardwareInformation.wiredIPv4Addresses
                }
            }
        }
        $apiUrl = $response.'@odata.nextLink'
    } while ($apiUrl)
    
    # Export the devices info to a CSV file
    $devicesInfo | Export-Csv -Path $csvPath -NoTypeInformation

Resources