Windows VM中管理员帐户的密码轮换

时间:2019-10-16 06:33:12

标签: azure-functions

我想在Azure中自动更改Windows vm中管理员帐户的密码。我的帐户中存在多个虚拟机,每个虚拟机的管理员帐户的密码应在30天后更改,并应保存在keyvault中。谁能帮我这个忙..

1 个答案:

答案 0 :(得分:0)

如果您考虑使用Azure automation powershell runbook进行操作,请尝试使用以下PowerShell更新Windows VM的新密码并将新密码保存到密钥库中:

$username = "<your Azure account>"
$passwd = "<your Azure account password>"
$keyVaultName = "<your key vault name>"

$secpasswd = ConvertTo-SecureString -String $passwd -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential ($username, $secpasswd)
Login-AzureRmAccount -Credential $cred -TenantId "<your tenant ID>" -SubscriptionId "<your subscription ID>"

#define new password 
$admin = "mgr" 
$adminPasswd = "password1234!"
$secpasswd = ConvertTo-SecureString -String $adminPasswd -AsPlainText -Force
$newCred = New-Object Management.Automation.PSCredential ($admin, $secpasswd)

#get all windows azure vms and set new password 
$VMs = Get-AzureRmVM | Where-Object {$_.StorageProfile.OsDisk.OsType -eq "Windows"}
foreach($vm in $VMs){
    Set-AzureRmVMAccessExtension -VMName $vm.Name -ResourceGroupName $vm.ResourceGroupName  -Name "enablevmaccess" -Credential $newCred 
}

#save new password into key vault 
Set-AzureKeyVaultSecret -VaultName  $keyVaultName -SecretValue $secpasswd -Name "NewPassword"  

如果您需要每30天执行一次此过程,则在发布运行记录之后create a schedule

请注意,在运行powershell命令之前,您应该了解两件事: 1.首先在胶库中导入ps模型: enter image description here 逐步导入这两个模块:

enter image description here 导入它需要一些时间。 enter image description here

这两个模块都可用后,它就完成了: enter image description here

  1. 确保用于登录的帐户有权将机密添加到密钥库中。如果没有权限,请follow this guide将访问策略映射到密钥库中: enter image description here

结果: 运行此ps runbbok之后,我的vm密码已更新,并且在密钥库中创建了新的机密: enter image description here

希望有帮助。