【问题标题】:How to start vm from different subscription using Azure Automation Account如何使用 Azure 自动化帐户从不同的订阅启动 vm
【发布时间】:2020-05-23 03:49:35
【问题描述】:

我在同一个租户中有三个订阅,比如 Sub1、Sub2 和 Sub3。我创建了自动化帐户在 Sub1 并且我的虚拟机在 Sub3。 (由于某些限制,无法在 Sub3 中创建自动化帐户)。我想编写一个将启动 Sub3 VM 的 powershell 脚本。

当我运行 Get-AzureRmSubscription 时,它只给我当前的订阅,即 Sub1 我的天蓝色自动化脚本如下 -

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

    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}

Get-AzureRmSubscription

 # $context = Get-AzureRmSubscription -SubscriptionId {subId}
# Set-AzureRmContext $context
# Start-AzureRmVM -ResourceGroupName "ResourceName" -Name "VMName"

你能指导我怎么做吗?

【问题讨论】:

    标签: azure azure-powershell azure-automation


    【解决方案1】:

    由于您的订阅位于同一租户中,因此您可以在 Sub3 中直接将 Azure RABC 角色分配给您的 Azure 自动化连接(服务主体)。然后就可以在Sub3

    中管理Azure资源了

    例如

    1. 获取连接应用程序 ID

    2. 分配角色

    Connect-AzAccount
     $sp=Get-AzADServicePrincipal -ApplicationId < the appId you copy>
    Set-AzContext -SubscriptionId <the id of sub3>
    #assign Contributor role to the connection at subsciprion level
    New-AzRoleAssignment -ObjectId $sp.id -RoleDefinitionName Contributor
    
    1. 测试
    $connectionName = "AzureRunAsConnection"
    try
    {
        # Get the connection "AzureRunAsConnection "
        $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         
    
        "Logging in to Azure..."
        Add-AzureRmAccount `
            -ServicePrincipal `
            -TenantId $servicePrincipalConnection.TenantId `
            -ApplicationId $servicePrincipalConnection.ApplicationId `
            -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
    }
    catch {
        if (!$servicePrincipalConnection)
        {
            $ErrorMessage = "Connection $connectionName not found."
            throw $ErrorMessage
        } else{
            Write-Error -Message $_.Exception
            throw $_.Exception
        }
    }
    
    Get-AzureRmSubscription
    

    【讨论】:

    • 感谢领导 Jim。我已经从您提供的脚本中添加和修改了必填字段。出现错误为“Connect-AzAccount:术语‘Connect-AzAccount’未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,或者如果包含路径,请验证路径正确,然后重试。在 line:2 char:1" 尝试通过将 Connect-AzAccount 更改为 Connect-AzureRmAccount 但仍然出现相同的错误。
    • @Oxygen 请使用您的 sub3 所有者的帐户在您的本地计算机上运行第 2 步脚本。
    • 这帮助了@Jim。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多