[英]Change password of Azure VM using PowerShell
I have tried this approach to change a password of an Azure VM:我尝试过这种方法来更改 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.当我console.log
$Credential.UserName
和$Credential.GetNetworkCredential().Password
的值时,我得到了我输入的用户名和密码的值。
What am I missing here?我在这里缺少什么?
I've never used Set-AzureVMAccessExtension
, but I've used the Az PowerShell equivalant Set-AzVMAccessExtension
.我从未使用过Set-AzureVMAccessExtension
,但我使用过 Az PowerShell Set-AzVMAccessExtension
。 It needs you to pass -Credential $Credential
instead of - UserName
and -Password
.它需要您传递-Credential $Credential
而不是 - UserName
和-Password
。
You can try this script I made a while ago to to reset passwords for Azure VMs:你可以试试我前段时间制作的这个脚本来重置 Azure VM 的密码:
# 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://docs.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://docs.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.我发现在您重新启动 VM 之前不会更新密码,因此需要Restart-VM
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.