2

I have tried this approach to change a password of an Azure VM:

$resgroup = "rsource1"
$vmName = "virtualmachine1"

$VM = Get-AzVM -ResourceGroupName $resgroup -Name $vmName 

$Credential = Get-Credential

$VM | Set-AzureVMAccessExtension    –UserName $Credential.UserName `
                                    –Password $Credential.GetNetworkCredential().Password

$VM | Update-AzVM

But I keep getting this error:

Object reference not set to an instance of an object.

When I console.log the values of $Credential.UserName and $Credential.GetNetworkCredential().Password I got the values of username and password that I have inputted.

What am I missing here?

1

2 Answers 2

3

I've never used Set-AzureVMAccessExtension, but I've used the Az PowerShell equivalant Set-AzVMAccessExtension. It needs you to pass -Credential $Credential instead of -UserName and -Password.

You can try this script I made a while ago to to reset passwords for Azure VMs:

# Replace these values with your own
$resourceGroupName = "Servers-RG"
$vmName = "server1"

# Get the VM into an object
$vm = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName 

# Store credentials you want to change
$credential = Get-Credential -Message "Enter your username and password for $vmName"

# Store parameters in a hashtable for splatting
# Have a look at https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-7
$extensionParams = @{
    'VMName' = $vmName
    'Credential' = $credential
    'ResourceGroupName' = $resourceGroupName
    'Name' = 'AdminPasswordReset'
    'Location' = $vm.Location
}

# Pass splatted parameters and update password
Set-AzVMAccessExtension @extensionParams

# Restart VM
# Don't need to pass any switches since they are inferred ByPropertyName
# Have a look at https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pipelines?view=powershell-7
$vm | Restart-AzVM

I found that the password update doesn't happen until you restart the VM, so Restart-VM is required.

Sign up to request clarification or add additional context in comments.

1 Comment

This is working for rdp administrator logins only right? Can I know the process of reset the rdp password for general user accounts?
1

If anyone interested in the Linux (KISS) version (no VM restart needed):

$settings = '{}'
$protectedSettings = '{
   "username": "<yourusername, prefer using Credentials object>",
   "password": "<yourpassword, prefer using Credentials object>"
}'        

Set-AzVMExtension `
    -VMName $vmName `
    -ResourceGroupName $rgName `
    -Location $location  `
    -Name "VMAccessForLinux"  `
    -Publisher "Microsoft.OSTCExtensions" `
    -ExtensionType "VMAccessForLinux"  `
    -TypeHandlerVersion "1.4"  `
    -Settingstring $settings `
    -ProtectedSettingString $protectedSettings

1 Comment

Isn't this going to modify other properties? Possible to use Get-AzVMExtension before?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.