Monday, March 23, 2015

Reset Azure VM admin password

Help I lost my Azure VM admin password!

It's a pretty common mistake people make. They create an Azure VM and forget to write down the Azure VM admin password. You obviously knew it at the time of creation but a few weeks later it might be hard to recall what you configured as your admin password... End result is most of the time recreating another Azure VM. Ok if it was just a little demo machine but pretty annoying if it's a production system.

Luckily Azure VM admin password can be reset through PowerShell. And in this post I'll just quickly demonstrate how to complete this pretty simple task. All you need is a couple of things, nothing overly complicated.

Required:
  • The latest Microsoft Azure PowerShell cmdlet. Documentation on how to install that can be found on the following page Azure PowerShell.
  • Azure service or co-administrator account
  • A powered on VM with the VM Agent installed!
  • And your new username and password!
Note: The Azure VM will be restarted during the process of resetting the Azure VM admin credentials.

The problem:

So the problem we are facing in this case is the inability to connect to an Azure VM. I myself have provisioned a Windows 10 image from the MSDN gallery and am unable to currently connect to the Azure VM as I managed to forget the password.. This is what that look like..

Unable to logon to Azure VM

How to resolve?

So great, we have a problem. We cannot log on to the Azure VM. How do we somehow regain access or reset the password. It's all achieved by the following two commands. They are pretty basic but very powerful. They are both ran through the Microsoft Azure PowerShell cmdlet. Scroll down and I'll take you through the process.

Step 1. Set the new credentials

$adminCredentials = Get-Credential -Message "Enter new Azure VM credentials"

Step 2. Reset the Azure VM administrator password

(Get-AzureVM) |
Where-Object -Property Status -EQ "ReadyRole" |
Select-Object -Property Name, ServiceName |
Out-GridView -Title "Select a VM …" -PassThru |
ForEach-Object {
    $VM = Get-AzureVM -Name $_.Name -ServiceName $_.ServiceName
    If ($VM.VM.ProvisionGuestAgent) {
        Set-AzureVMAccessExtension -VM $VM `
            -UserName $adminCredentials.UserName `
            -Password $adminCredentials.GetNetworkCredential().Password `
            -ReferenceName "VMAccessAgent" |
        Update-AzureVM
        Restart-AzureVM -ServiceName $VM.ServiceName -Name $VM.Name
    } else {
        Write-Output "$($VM.Name): VM Agent Not Installed"
    }
}



Walk through

1. So before we go ahead, you need to launch your Microsoft Azure PowerShell cmdlet and connect to the subscription which has the Azure VM.


Connect to subscription


2. Followed by this is configuring the new Azure VM admin credentials by running the command below. This will prompt you to enter the new credentials.
$adminCredentials = Get-Credential -Message "Enter new Azure VM credentials"

Set credentials


3. Run $adminCredentials if you want to confirm that it's configured. And yes the password is safely stored and won't be displayed.

Check variable

4. Now it's time to reset the Azure VM password. Run the script that has included at the top of the post. You will be prompted with a grid view to select the VMs. The script only retrieves Azure VMs that are powered on as a password reset cannot be completed on a Azure VM that is shut down.

PowerShell script to reset Azure VM password


5. You have to be a little patient as it does take a while. The Azure VM is at the end of the process updated and restarted.

Reset Azure VM admin password


6. Have a look in the Azure portal and you'll find that the extension on the Azure VM are being installed. This is obviously to reset the password, so wait till this process has completed.

Installing Extensions



7. Once the Azure VM has returned to the status Running, try and reconnect using the newly configured Azure VM admin username and password.

Successfully reset the Azure VM credentials






No comments:

Post a Comment