gpt4 book ai didi

azure - 使用 PowerShell 更改 Azure VM 的密码

转载 作者:行者123 更新时间:2023-12-02 06:41:48 24 4
gpt4 key购买 nike

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

但我不断收到此错误:

Object reference not set to an instance of an object.

当我 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://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

我发现密码更新只有在重新启动虚拟机后才会发生,因此需要 Restart-VM

关于azure - 使用 PowerShell 更改 Azure VM 的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60774530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com