Azure VM: reset local user account password

Arif Usman 421 Reputation points
2022-08-19T03:20:06.357+00:00

folks, I have about 200 az vm, contains local user account. I need to reset password for all vms. Now I can do it through portal fine, but like to do scripting.

I tried with
$Computer = "TestVM"
$vmcomp = Get-AzVM -Name $Computer
$vmName = $vmcomp.Name
$resourceGroupName = $vmName.ResourceGroupName
$location = $vmName.Location
$Password = "*P@ssword2022!!"
$UserName = "AzureVM"

        Set-AzureRmVMAccessExtension -ResourceGroupName $resourceGroupName -VMName $vmName -Name $name -Location $location -typeHandlerVersion "2.0" -ForceRerun  

but didn't work, can someone give me examples or any ideas here

Thanks In Advance......

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,287 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 98,626 Reputation points MVP
    2022-08-21T07:40:30.653+00:00

    Hi @Arif Usman ,

    The Az is the newer module and the replacement for the older AzureRM module.
    I would recommend to use the Az only.
    https://learn.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-8.2.0

    To uninstall the AzureRM module you could try the Uninstall-AzureRM cmdlet (which is in the Az.Account module): https://learn.microsoft.com/en-us/powershell/module/az.accounts/uninstall-azurerm?view=azps-8.2.0

    Some more useful information you will find here:
    https://basvo.github.io/azure/azurerm/powershell/2021/03/05/remove-azurerm-before-installing-az-module.html
    https://stackoverflow.com/questions/34204373/how-to-clean-up-bad-azure-powershell-uninstall

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

10 additional answers

Sort by: Most helpful
  1. Arif Usman 421 Reputation points
    2022-08-20T16:48:15.583+00:00

    @Andreas Baumgarten thank you for your reply.
    Two things, what if i don't know password for vm. In portal i can easily reset password.
    2nd. I am getting following error message when i ran set-azvmaccessextention
    233097-set-azvmaccessextenstion.png

    So i tried following from https://learn.microsoft.com/en-us/azure/virtual-machines/automatic-extension-upgrade

    Set-AzVMExtension -ExtensionName "Microsoft.Azure.Monitoring.DependencyAgent" `  
        -ResourceGroupName "myResourceGroup" `  
        -VMName "myVM" `  
        -Publisher "Microsoft.Azure.Monitoring.DependencyAgent" `  
        -ExtensionType "DependencyAgentWindows" `  
        -TypeHandlerVersion 9.5 `  
        -Location WestUS `  
        -EnableAutomaticUpgrade $true  
    

    no luck

    0 comments No comments

  2. Andreas Baumgarten 98,626 Reputation points MVP
    2022-08-20T17:26:55.477+00:00

    Hi @Arif Usman ,

    please take care about the different modules! Az module is not compatible with the AzureRM module!
    Use only one of the modules.
    Also make sure you are using the latest version of the PowerShell module.

    Not sure what do you mean with "what if i don't know password for vm" ?
    You are setting the new password ( $Password = "*P@ssword2022!!" ) of the local user ( $UserName = "AzureVM" ). That's it.

    If you like to try a different approach.
    This is an option to change the password of a local user in a VM as well: https://github.com/abaumgarten42/Azure_Stuff/tree/main/Azure_VMs/RunPowerShellScriptsOnVMs
    You need 2 PowerShell scripts on you computer:
    RunPSonVM-Reset-PwLocalUser.ps1 starts the PS script Reset-PwLocalUser.ps1 on the defined Azure VM and resets the password of the defined local user

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  3. Arif Usman 421 Reputation points
    2022-08-21T05:29:04.54+00:00

    @Andreas Baumgarten : would you please give little tips how can run either of module. I had remove az and tried azurerm no luck. unable to remove AzureRm.

    thanks in advance.

    0 comments No comments

  4. Arif Usman 421 Reputation points
    2022-08-21T14:53:52.71+00:00

    still getting conflict message.
    233262-image.png

    0 comments No comments