Script- Reset Azure VM Local Administrator credential without login to VM or Azure Portal

Table of Contents

  1. Purpose
  2. Script Overview
  3. Pre-requisites
  4. Pre-checks and readiness
    4.1 User list before any changes
    4.2 PowerShell Module readiness
  5. PowerShell Script
  6. Script Execution
    6.1 Create directory and copy the script
    6.2 Final execute of script
  7. How to reset Local Administrator password from Azure portal
  8. End of Document

PowerShell Script-Reset Azure VM Local Administrator Credential without login to VM or Azure portal

1. Purpose

Purpose of this document is to explain how to re-set local administrator credential of Azure VM via PowerShell (without login to VM or without login to Azure portal).

2. Script Overview

If you forgot Local administrator password of your Azure VM, don’t worry use below script to reset Local administrator password of Azure VM via PowerShell.

  • Script will first display Azure subscription ID, Resource Group, VM name, VM location.
  • Script will prompt on which VM you want to perform action (Prompt Subscription ID, Resourge group, VM name and VM location).
  • If local user already exist with name (ex: administrator3) in VM, script will reset the password of administrator3 and provide Local Administrator access.
  • If Local user does not exist (ex: administrator3) in VM, script will create new local user with name administrator3 and provide Local Administrator access. You can put username according to your choice.

3. Pre-requisites

  • Azure credential with sufficient permission
  • PowerShell module “AZ”

4. Pre-checks and readiness

4.1 User list before any changes

You already forgot your Local Administrator credential, so you can’t do this check. Not require step, just to explain the situation.

4.2 PowerShell Module readiness

  • Check status of installed PowerShell modules in your Laptop/Desktop

Above status shows no PowerShell modules installed.

  • Install PowerShell module “AZ”
  • Azure module “AZ” status

You can see AZ PowerShell module is installed and ready to use.

5.     PowerShell Script

Below is the PowerShell script to reset Local Administrator password and add as Local Administrator user.

  • Script will connect to Azure and show subscription and VM detail.
  • Script will prompt below things.
    • Subscription ID where the VM is.
    • ResourceGroup name where the VM is.
    • VM name
    • VM Location

PowerShell Script Start

# Owner - Niraj Srivastava
# Version 1
# Date 01-Dec-2019
# Reset local administrator password for Azure VM
cls
Connect-AzAccount
# To dispay all the Subscription details you have
Get-AzSubscription | select Name, Id, TenantID | ft

# Provide the subscription ID where your VM exist
$subID = Read-Host -Prompt "Please enter subscription ID :"


Select-AzSubscription -SubscriptionId $SubID 

#Dispay the VM's list under subscription you selected
Get-AZvM | ft

#Provide the ResouceGroup where VM exist
$RgName = Read-Host -Prompt "Enter the ResourceGroup Name :"
#Provide the VM name
$VmName = Read-Host -Prompt "Enter the VM name for which Local-Admin credential to be changed:"
#Provide the VM location
$Location = Read-Host -Prompt "Enter VM Location "

#Reset existing user credential or if user not exist create and make local administrator
Set-AzVMAccessExtension -ResourceGroupName $RgName -Location $Location -VMName $VmName -Credential (get-credential) -typeHandlerVersion "2.0" -Name enablevmaccess

Script End

6. Script Execution

6.1 Create directory and copy the script

6.2 Final execute of script

  • Provide Azure login credentials (azure ID should have sufficient rights to see the subscription and perform the action on the Azure VM)

  • Script will display the subscriptions. If you have multiple subscriptions you will see all the subscriptions.
  • Provide the subscription ID where the VM is.
  • Script will display the VM’s detail. Based on you input in above step.
  • Script will prompt for ResourceGroup Name, VM name and the VM location, for which need to reset local administrator password.
  • Script will pop-up for username and password for which you want to reset the credential.
  • Put the username as show below(Do not used computer name before username NSIT-VM01\administrator3, instead simply user “administrator3”)
  • Provide new password for user.

This will reset password of “administrator3” if exist, else it will create new user “administrator3” and add in Local Administrators group.

New local administrator available

You can now login with user having Local Administrator rights.

7. How to reset Local Administrator password from Azure portal

How to re-set Azure VM Local administrator credential or create new Azure VM Local administrator via Azure portal.

See category “Tips and Tricks” on https://nsitautomation.in/

Direct link: https://nsitautomation.in/reset-azure-vm-local-administrator-credential/

8. End of Document

Facebook Comments

2 thoughts to “Script- Reset Azure VM Local Administrator credential without login to VM or Azure Portal”

  1. Alankar Srivastava says:

    Awesome content. Step by step instructions . This really helpful who are new to the Azure environment.

    1. Niraj Srivastava says:

      Thank you Alankar.

Leave a Reply

Your email address will not be published. Required fields are marked *