Hi @Arif Usman ,
Set-AzureRmVMAccessExtension
(AzureRM module) or Set-AzVMAccessExtension
(Az module) using the -Credential
parameter to provide username and password.
https://learn.microsoft.com/en-us/powershell/module/azurerm.compute/set-azurermvmaccessextension?view=azurermps-6.13.0
https://learn.microsoft.com/en-us/powershell/module/az.compute/set-azvmaccessextension?view=azps-8.2.0
On both websites the -Credential
parameter has the same description:
-Credential
Specifies the user name and password for the virtual machine as a PSCredential object. If you type a different name than the current local administrator account on your VM, the VMAccess extension will add a local administrator account with that name, and assign your specified password to that account. If the local administrator account on your VM exists, it will reset the password and if the account is disabled, the VMAccess extension enables it. To obtain a credential, use the Get-Credential cmdlet. For more information, type Get-Help Get-Credential.
There is one thing you should be aware when using Set-AzVMAccessExtension
/Set-AzureRmVMAccessExtension
:
As described the Set-AzVMAccessExtension
will add the user as an administrator if the account does not exist on the VM. If the user account already exists the user must be a member of the local Administrators
group or the extension will fail.
If this is ok for you please give this a try (tested a few minutes ago):
$UserName = "AzureVM"
$Password = "*P@ssword2022!!"
$Computer = "TestVM"
$secPassword = ConvertTo-SecureString $Password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($userName, $secPassword)
$vmcomp = Get-AzVM -Name $Computer
$vmName = $vmcomp.Name
$resourceGroupName = $vmcomp.ResourceGroupName
$location = $vmcomp.Location
Set-AzVMAccessExtension -Credential $cred -Location "$location" -Name "PasswordReset" -ResourceGroupName "$resourceGroupName" -TypeHandlerVersion '2.4' -VMName "$vmName"
----------
(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)
Regards
Andreas Baumgarten