azure - 使用托管标识启动 Azure VM Runbook

标签 azure azure-powershell azure-managed-identity

我正在尝试运行 Runbook 以使用托管标识启动 VM。使用 runas 帐户可以正常工作,但因为 Microsoft 表示建议使用托管身份,所以我想尝试一下。

这是我从互联网上获得的脚本的一部分,适用于 runas 帐户:

$ResourceGroupName = 'test' 
$AzureVMName = 'test'   

$connectionName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

    "Logging in to Azure..."
    Login-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
}

这是我现在尝试使用但不起作用的脚本:

Connect-AzAccount -Identity
 
$ResourceGroupName = 'test' 
$AzureVMName = 'test'   

"Starting Azure VM..."
Start-AzureRmVM -Name $AzureVMName -ResourceGroupName $ResourceGroupName

我总是收到以下错误:

环境

{[AzureChinaCloud、AzureChinaCloud]、[AzureCloud、AzureCloud]、[AzureGermanCloud、AzureGermanCloud]、[AzureUSGovernme... 正在启动 Azure 虚拟机... 运行Login-AzureRmAccount进行登录。

我尝试了很多方法,但无法使其正常工作......

最佳答案

Thomas已经建议您在下面的代码中使用 2 个模块,即 "Az""AzureRM" :

Connect-AzAccount -Identity
 
$ResourceGroupName = 'test' 
$AzureVMName = 'test'   

"Starting Azure VM..."
Start-AzureRmVM -Name $AzureVMName -ResourceGroupName $ResourceGroupName

因此,仅使用 Az 模块,如下所示:

Connect-AzAccount -Identity
$ResourceGroupName = 'ansumantest' 
$AzureVMName = 'testVM'   

"Starting Azure VM..."
Start-AzVM -Name $AzureVMName -ResourceGroupName $ResourceGroupName

我在我的环境中使用一个具有托管身份的虚拟机来启动另一台虚拟机进行了测试。

输出:

enter image description here

引用:

Install the Azure Az PowerShell module | Microsoft Docs

关于azure - 使用托管标识启动 Azure VM Runbook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69000813/

相关文章:

security - IP 限制 Azure VM 角色

azure - 在 Azure 中启动/停止虚拟机的最低权限角色

azure-service-fabric - Service Fabric 重启应用程序

使用用户分配的托管ID的 azure 函数 - 无法写入存储队列

azure - 适用于 Azure Active Directory 的 ARM 模板

azure - 使用 Azure 流量管理器进行扩展的数据库策略

.net - 无法将项目转换为 Azure 2.6 - VS 2012

azure - 如何绕过 Backup-SqlDatabase 提示输入密码?

azure - 使用托管标识从应用服务中提取 Azure 容器注册表镜像

azure - ADF 发布无法查找托管身份