Rename-AzVm.psm1

############Function Rename-VM Azure##############
############Baptiste COUAVOUX / CEGID#############
############02/12/2019####################

#This function will only rename the displayed name of the VM on the Azure portal.
#This does NOT change the hostname of the VM
#Process used :
#- Delete original VM resource
#- Get properties of attached resource of the original VM
#- Recreate the VM with same config but with a displayed new name (bypass the 15 imposed characters)


function Rename-AzVM
{

Param(
    [parameter (Mandatory=$true)]
    [object] $OriginalVmName,
    [parameter (Mandatory=$true)]
    [object] $NewVmName,
    [Parameter(Mandatory=$true)]
    [string] $SouscrAzure
)

#Connection souscription with Azure account
Connect-AzAccount
Set-AzContext -Subscription $SouscrAzure

#Get original VM properties
$Properties_VM = Get-AzVM -Name $OriginalVmName -erroraction 'silentlycontinue'
$ID_VM = $Properties_VM.id


if (!$ID_VM)
    {
        #If VM doesn't exist, exit
        write-Output "VM $OriginalVmName does not exist"
        Exit
    }
else
    {

        #Delete original VM resource
        write-Output "Delete VM resource [$OriginalVmName] .."
        Remove-AzResource -ResourceId $ID_VM -Force


        #Get original VM resource properties (Location,RG,Network,OSdisk,DataDisk,Size)
        write-Output "Get config from VM [$OriginalVmName] .."
        $Location = $Properties_VM.Location
        $RG = $Properties_VM.ResourceGroupName
        $ID_NETWORK = $Properties_VM.NetworkProfile.NetworkInterfaces.Id
        $ID_OSDISK = $Properties_VM.storageProfile.OsDisk.ManagedDisk.Id
        $ID_DATADISKS = $Properties_VM.storageProfile.DataDisks.ManagedDisk.Id
        $VM_Size = $Properties_VM.HardwareProfile.VMSize


        
        #Create new VM config with original size and new name
        $VirtualMachine = New-AzVMConfig -VMName $NewVmName -VMSize $VM_Size
        #Set the original OSDisk in config
        $VirtualMachine = Set-AzVMOSDisk -VM $VirtualMachine -ManagedDiskId $ID_OSDISK -CreateOption Attach -Windows

        #Add mutliple,one or none original DataDisk resource
        if (!$ID_DATADISKS)
                {
                }
            else
                {
                    if ($ID_DATADISKS.Gettype().isArray)
                        {  
                        $i=0
                            foreach ($ID_DATADISK in $ID_DATADISKS)

                                {

                                $VirtualMachine = Add-AzVMDataDisk -VM $VirtualMachine -ManagedDiskId $ID_DATADISK -CreateOption Attach -Lun $i
                                $i++

                                    
                                }
                        }
                    else
                        {
                                $VirtualMachine = Add-AzVMDataDisk -VM $VirtualMachine -ManagedDiskId $ID_DATADISKS -CreateOption Attach -Lun 0
                                
                        }
                }


                    #Add original Network Interface
                    $VirtualMachine = Add-AzVMNetworkInterface -VM $VirtualMachine -Id $ID_NETWORK


                    #Create new VM with config
                    write-Output "Create VM [$NewVmName] from config .."
                    New-AzVM -VM $VirtualMachine -ResourceGroupName $RG -Location $Location

                    

    }

    }



    #Ex : Rename-AzVM -OriginalVmName test -NewVmName testrename -souscrAzure testsousc