How to keep / set the default admin password in Azure ASM after a copy of the OS Disk

189 Views Asked by At

I need to migrate old Classic VMs to New (still) Classic VMs in order to use :

  • New Kind of VMs (Standard_A to Standard_DS_V2)
  • Premium Disks

I've made a script which is :

  • Stop the existing VM
  • Delete the existing VM
  • Copy the old disks (OS + Data) to Premium Storage Account
  • Register the New Disks

Then I have another script to create the new VM based on Json informations This script has been developped a while ago to create VMs based on the Json informations and existing Azure Disk Template.

I've adapted it to create a new VM based on the copy of the disks.

Everything is working quite well except the point of the admin user / password. Each time I create a new VM, I can't access it with my old admin user (the one used to create the VM initially). In order to access the VM, I need to use the Azure portal to reset the password Then I can access the VM.

The thing is that I want to either :

  • do this step into my script
  • do not have to do this at all (we're using a copy of the OS Disk / Data Disks so it shouldn't be needed)

Can someone explain me how to manage this ? I'm on it since days / weeks and I can't find a way to do it properly.

The actions made by my script for the VM creation (ubuntu VM):

- Set-AzureSubscription -CurrentStorageAccount $VM.StorageAccount -SubscriptionId $VM.SubscriptionId
- $GlobalOSDiskName = (Get-AzureDisk | Where-Object { $_.OS -iin "Linux","Windows" } | Select DiskName).DiskName
- $VM = Get-Content $json_file -Raw | ConvertFrom-Json
- $VMOSDisk = $VM.OSDisk
- $vmObj = New-Object -TypeName Microsoft.WindowsAzure.Commands.ServiceManagement.Model.PersistentVM
- $vmObj = New-AzureVMConfig -Name $VM.Name -InstanceSize $VM.InstanceSize -DiskName $VMOSDisk.DiskName
- $vmObj = Set-AzureOSDisk -VM $vmObj 
- $VMDataDisks = $VM.DataDisks
- foreach ($VMDataDisk in $VMDataDisks) {
    $vmObj = Add-AzureDataDisk -Import -DiskName $VMDataDisk.DiskName -LUN $VMDataDisk.Lun -HostCaching $VMDataDisk.HostCaching -VM $vmObj
    }
- $password = Read-Host -Prompt "Enter password for admin account '$username'" -AsSecureString 
- $vmObj = Set-AzureVMAccessExtension -VM $vmObj -UserName $username -Password $password
    # This is the last test I've made for this but it's not better...
    # Adding informations for Subnet, StaticIP, AvailabilitySet, EndPoint, ACL etc...
- New-AzureVM -VNetName $VM.Vnet -ServiceName $VM.ServiceName -VM $vmObj -WaitForBoot
    # Creation of the VM based on the object $vmObj

==> The VM is created correctly but cannot be accessed

Moreover, when I reset the password with the Azure Portal, I'm prompted to fill my password when I use a sudo command when I was not prompted on the old VM.

That's not necessary a big deal, but I'd like to know why and how to enable / disable this.

Edit : I've tested the suggestion written in this doc which seems to work.

This method consists in :

  • Export the config of the source VM with command Export-AzureVM into an xml file
  • Edit the exported xml file to match the desired settings
  • Create a new VM Object based on this with command Import-AzureVM

The result seems to work properly :

  • The VM is well created with all the parameters
  • The Admin password remains the same without having set it anywhere

The concerns about this is that :

  • There is no reference in the exported xml to admin credentials
  • So there is no reference in the new vm object to admin credentials
  • The object created is exactly the same the one I create in my way

Can someone explain me the difference between both solutions ?

Thanks for your help

1

There are 1 best solutions below

0
On

Finally after several testing, I still don't have any confirmation of anything...

In some cases, the admin account password needs to be reset, in other cases not...

If someone has an answer It would be very helpful...