繁体   English   中英

使用 PowerShell 更改 Azure VM 的密码

[英]Change password of Azure VM using PowerShell

我尝试过这种方法来更改 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

但我不断收到此错误:

你调用的对象是空的。

当我console.log $Credential.UserName$Credential.GetNetworkCredential().Password的值时,我得到了我输入的用户名密码的值。

我在这里缺少什么?

我从未使用过Set-AzureVMAccessExtension ,但我使用过 Az PowerShell Set-AzVMAccessExtension 它需要您传递-Credential $Credential而不是 - UserName-Password

你可以试试我前段时间制作的这个脚本来重置 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

我发现在您重新启动 VM 之前不会更新密码,因此需要Restart-VM

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM