Functions/Rename-IntuneDevices.ps1



function Rename-IntuneDevices {
    [CmdletBinding(SupportsShouldProcess)]
    param (
        [Parameter()] [string] $DisplaynamePrefix,
        [Parameter()] [string] $DisplaynameSuffix,
        [Parameter(Mandatory)] [string] $CsvFile,
        [Parameter()] [string] $Filter,
        [Parameter()] [switch] $Reboot
    )


    if (-not(Get-MgContext)) {
        throw "MgGraph not connected, please connect with Connect-MgGraph"
    }

    if ((Get-MgProfile).Name -ne "beta") {
        Select-MgProfile beta
    }


    if (-not(Get-MgDeviceManagementManagedDevice -Top 1)) {
        throw "No devices found"
    }


    $Result = @()


    $Config = @{
        Prefix        = $DisplaynamePrefix
        Suffix        = $DisplaynameSuffix
        SourceCSVFile = $CsvFile
        LocalCSVFile  = "$($env:TEMP)\DevicesSerialNumberAndAssetTag.csv"
    }


    Remove-Item $Config.LocalCSVFile -Force -ea 0 -WhatIf:$false
    Start-BitsTransfer -Source $Config.SourceCSVFile -Destination $Config.LocalCSVFile -ErrorAction Stop -WhatIf:$false

    $DeviceImport = Import-Csv $Config.LocalCSVFile
    if (-not($DeviceImport)) {
        throw "CSV import unsuccesfull"
    }

    $Param = @{ All = $true }
    if ($Filter) {
        $Param.Filter = $Filter
    }
    # $Param
    $IntuneDevices = Get-MgDeviceManagementManagedDevice @Param

    # $IntuneDevices | Select-Object DeviceName, DeviceType, Manufacturer, Model, SerialNumber, UserPrincipalName | ft


    foreach ($c in $DeviceImport) {

        $NewName = "$($Config.Prefix)$($c.AssetTag)$($Config.Suffix)"

        if ($IntuneDevices.SerialNumber -contains $c.SerialNumber) {

            $CurrentDevice = $IntuneDevices | Where-Object SerialNumber -EQ $c.SerialNumber

            $r = [PSCustomObject]@{
                Result            = ""
                Id                = $CurrentDevice.Id
                CurrentName       = $CurrentDevice.DeviceName
                NewName           = $NewName
                DeviceType        = $CurrentDevice.DeviceType
                Manufacturer      = $CurrentDevice.Manufacturer
                Model             = $CurrentDevice.Model
                SerialNumber      = $CurrentDevice.SerialNumber
                UserPrincipalName = $CurrentDevice.UserPrincipalName
                Rebooted          = $false
            }

            $PendingRename = Get-MgDeviceManagementManagedDevice -ManagedDeviceId $CurrentDevice.Id | Select-Object -ExpandProperty DeviceActionResults | Where-Object { $_.ActionName -eq "setDeviceName" -and $_.ActionState -eq "pending" }
            # $PendingRename
            if ($PendingRename) {

                $r.Result = "RenamePending"

            } elseif ($CurrentDevice.DeviceName -ne $NewName) {

                if ($PSCmdlet.ShouldProcess("$($CurrentDevice.Id) - $($CurrentDevice.DeviceName) - $($NewName)", "Set-MgDeviceManagementManagedDeviceName")) {
                    Set-MgDeviceManagementManagedDeviceName -ManagedDeviceId $CurrentDevice.Id -DeviceName $NewName

                    $r.Result = "Renamed"
                    if ($Reboot) {
                        Restart-MgDeviceManagementManagedDeviceNow -ManagedDeviceId $CurrentDevice.Id
                        $r.Rebooted = $true
                    }

                } else {
                    $r.Result = "WillRename"
                }
            } else {
                $r.Result = "NoRenameNeeded"
            }

            if ($r.Result -ne "NoRenameNeeded") {
                $Result += $r
            }
        }
    }

    $Result = $Result | Sort-Object Result

    return $Result

}