我在同一租户中有三个订阅,即 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资源
例如
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
关于azure - 如何使用 Azure 自动化帐户从不同订阅启动虚拟机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61967189/