azure - 如何使用 Azure 自动化帐户从不同订阅启动虚拟机

标签 azure azure-powershell azure-automation

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

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

$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 RABC 角色分配给 Sub3 中的 Azure 自动化连接(服务主体)。然后您就可以在Sub3中管理Azure资源

例如

  1. 获取连接应用程序 ID enter image description here

  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
  • 测试
  • $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
    

    enter image description here

    关于azure - 如何使用 Azure 自动化帐户从不同订阅启动虚拟机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61967189/

    相关文章:

    javascript - 通过复杂类型 cosmosDB azure 的存储过程进行批量更新

    azure - 在 Azure 中找不到 VirtoCommerce 缩略图

    azure - Get-AzApiManagementSubscription 命令返回订阅列表,但键值为空

    azure - 使用 Azure 资源管理器模板将现有 Web 服务计划附加到新网站

    Azure 自动电子邮件通知 ApplicationLog 中的错误

    php - 在 azure web 应用程序上使用 laravel

    azure - 无法安装 NServiceBus 主机 Windows 服务

    azure - 通过 azure Automation powershell 禁用/启用 Azure 监视器警报规则

    powershell - 循环遍历 Azure 对象并筛选标签

    Azure Powershell - 自动化登录-AzureRmAccount AD 登录 - 用于 Azure 功能