Forum Discussion

torquetechit_tonyd's avatar
torquetechit_tonyd
Brass Contributor
Mar 23, 2021

Device Rename in HAADJ environment

Hi,

Wondering if there is a supported process for the renaming of devices which have been Hybrid Joined.

 

As we know the device naming setup is currently limited to only being able to provide a per-determined prefix for devices which then has a randomly generated suffix of letters and characters added to it.

 

When deploy machines, currently, there is no easy way to associate a machine with the machines serial number or other company asset tag type requirements.

 

I would assume the rename process would probably be triggered by the device first being renamed via the on premise AD environment and this change then being synced to AAD via a sync process. Is this correct? 

 

Also, if a machine is redeployed, how would the previously assign machine name be re-assigned to the same device to prevent additional unwanted orphaned device be left around in both AAD and on premise AD.

 

I look forward to your helpful advise and assistance.

 

Cheers

Tony

8 Replies

  • Durrante's avatar
    Durrante
    Brass Contributor
    Hope this helps: https://oofhours.com/2020/05/19/renaming-autopilot-deployed-hybrid-azure-ad-join-devices/
    • Targetpractice's avatar
      Targetpractice
      Copper Contributor
      The problem with Michaels script is that if the object currently exists in your on-prem AD it fails to rename the computer which will require someone to go into AD and remove it manually.
      • torquetechit_tonyd's avatar
        torquetechit_tonyd
        Brass Contributor
        Thanks @Targetpractise and Durrante for you replies.

        I am testing out a combination of the scripts as we are not using SCCM for device co-management.

        Basically, combining the component where it get the device serial number to create the new device name, then checks if it exists already in AD and continues.

        It would be good to have an idea when the naming conventions for HAADJ will be updated to enable the use of %Serial% as per a cloud only joined machine.

        Cheers
        Tony
  • Targetpractice's avatar
    Targetpractice
    Copper Contributor

    Tony, 

     

      If you have SCCM at your site you can create a task sequence to run on the computer that will run a PowerShell script. The collection looks for computers with the pre-fix as part of the computer name (like AUTOPILOTPCXXXX) The script will query the BIOS to see if an asset tag is programed into it and then rename the computer to me new prefix+Asset tag. Now the computer does have to be on the network, VPN or in the office as the script is updating the AD record which will then get updated on the Azure/Intune side. The script will search AD to see if a computer object with the same name exists remove it if found, you can also have it search for the computer in SCCM and remove it

     

    Its not perfect but its been working for me. 

     

    Set-ExecutionPolicy Bypass -Scope Process -Force
    $oldCompName = $env:COMPUTERNAME
    $AssetTag = (Get-WmiObject Win32_SystemEnclosure).SMBiosAssetTag
    $serial = Get-WmiObject win32_bios | select -expand serialnumber
    $sccmServer='SCCMSERVERNAME'
    $sccmSite='SITECODE'

    Reset-ComputerMachinePassword
    Start-Transcript "C:\Windows\PKGCache\LOG\RenameComputer.log" -Append

    If (($AssetTag) -and ($AssetTag -ne "No Asset Information"))
    {
    $newname = "PREFIX" + $AssetTag -replace '[^a-zA-Z0-9]', ''
    #Rename-Computer -NewName $newname -Force
    }
    Elseif ((!$AssetTag) -or ($AssetTag -eq "No Asset Information"))
    {
    $newname = "PREFIX" + $serial -replace '[^a-zA-Z0-9]', ''
    #Rename-Computer -NewName $newname -Force
    }

    $validname = $newname.substring(0, [System.Math]::Min(14, $newname.Length))
    Write-host "Old Computer name $oldcompname"
    Write-host "New computer name $validname"

    if ($validname -eq "PREFIX")
    {$validname = "PREFIX" + (Get-random -Maximum 1000000)}
    else
    {Write-Host "Computer is named $validname, proceeding"}

    # find and delete the computer from AD
    If ($env:COMPUTERNAME -eq $Validname)
    {Stop-Transcript
    Exit}

    ELSE
    {$dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
    $root = $dom.GetDirectoryEntry()
    $search = [System.DirectoryServices.DirectorySearcher]$root
    $search.filter = "(&(objectclass=computer)(name=$validname))"
    $search.findall() | %{$_.GetDirectoryEntry() } | %{$_.DeleteObject(0)}}

    Start-Sleep -seconds 60


    # Rename computer to new name
    If ($env:COMPUTERNAME -eq $validname)
    {Stop-Transcript
    Exit}
    Else
    {Rename-Computer -NewName $validname -Force}

    Stop-Transcript