【问题标题】:Automation Runbook not able to set default subscription on runAutomation Runbook 无法在运行时设置默认订阅
【发布时间】:2018-01-03 08:49:58
【问题描述】:

克隆了一个示例非经典 Runbook 并尝试将订阅设置为默认值。这给了我以下错误:

Select-AzureSubscription : The subscription name {nameofsubscription} doesn't exist.
Parameter name: name
At step1_validate:18 char:18
+ 
+ CategoryInfo          : CloseError: (:) [Select-AzureSubscription], ArgumentException
+ FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.Profile.SelectAzureSubscriptionCommand

如何选择或将特定订阅设置为默认订阅?以下方法抛出上述错误:

方法 1

Select-AzureSubscription -SubscriptionName $defaultSubscriptionname –Default 

Get-AzureSubscription -SubscriptionId 123XXXXXXXXXXXXXXXXXX96eXX58 | Select-AzureSubscription

方法 2

$subscriptionId = (Get-AzureRmSubscription | Out-GridView -Title 'Select Azure Subscription:' -PassThru).Id
Select-AzureRmSubscription -SubscriptionId $subscriptionId

尝试:要获取特定资源名称并进行配置更改,或使用 Get-Azurewebsite cmdlet,设置订阅似乎是强制性的。

注意:希望将自动化帐户中使用的服务主体添加到默认订阅可能会有所帮助,我还尝试将 ApplicationId 添加到订阅的访问控制中作为贡献者。虽然这里提到了我的 Runbook 和 WebApps 在同一个订阅中。参考:https://blogs.technet.microsoft.com/knightly/2017/05/26/using-azure-automation-with-multiple-subscriptions/#comment-1555(我的情况是两者都在同一个订阅中)

编辑:粘贴代码

<# 
This PowerShell script was automatically converted to PowerShell Workflow so it can be run as a runbook.
Specific changes that have been made are marked with a comment starting with “Converter:”
#>
<#
.DESCRIPTION
    To watch php version old in app, and if found turn it off and apply 5.6 

.NOTES
    AUTHOR: HBala
    LASTEDIT: Jan 04, 2018
#>

workflow step1_validate {

# Converter: Wrapping initial script in an InlineScript activity, and passing any parameters for use within the InlineScript
# Converter: If you want this InlineScript to execute on another host rather than the Automation worker, simply add some combination of -PSComputerName, -PSCredential, -PSConnectionURI, or other workflow common parameters (http://technet.microsoft.com/en-us/library/jj129719.aspx) as parameters of the InlineScript
inlineScript {
    $connectionName = "AzureRunAsConnection"
    $myResourceGroupName = "DevstorageRG"
    $defaultSubscriptionname = "StandardDevStaging"
    $newPhpVersion = "5.6"

    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 all ARM resources from all resource groups
    $ResourceGroups = Get-AzureRmResourceGroup 

    foreach ($ResourceGroup in $ResourceGroups)
    {    
        Write-Output ("Showing resources in resource group " + $ResourceGroup.ResourceGroupName)

        if( $ResourceGroup.ResourceGroupName -eq $myResourceGroupName ){
            $Resources = Find-AzureRmResource -ResourceGroupNameContains $ResourceGroup.ResourceGroupName | Select ResourceName, ResourceType
            ForEach ($Resource in $Resources)
            {

                    Write-Output ($Resource.ResourceName + " of type " +  $Resource.ResourceType)

                    Write-Output ($Resource.ResourceName + " of type " +  $Resource.ResourceType)

                    #Switch-AzureMode AzureServiceManagement
                    # Local powershell connects and works perfect with what I wanted to achieve.
                    # On Automation account, Runbook migration, it threw Azure subcription not set error for Set-AzureWebsite. 
                    # so remove rest of the code and focused on few options to get the default as below
                    # which led me to post the thread.
                    # 
                    # Approach 1
                    Select-AzureSubscription -SubscriptionName $defaultSubscriptionname –Default 
                    Get-AzureSubscription -SubscriptionId 1238XXXXXXXXXXXe5XXXX8 | Select-AzureSubscription

                    # Approach 2
                    $subscriptionId = (Get-AzureRmSubscription | Out-GridView -Title 'Select Azure Subscription:' -PassThru).Id
                    Select-AzureRmSubscription -SubscriptionId $subscriptionId

                    Write-Output ( "==============Subscription ID :===========")
                    Write-Output( $subscriptionId)

                    # Wants to update config / php version parameters.
                    Set-AzureWebsite -Name $Resource.ResourceName -HttpLoggingEnabled 1 -PhpVersion 5.6

                    # Had tried this as well which was suggested by Jason. But appears not working.. 
                    # looks like I have messed it up.                        
                    Get-AzureRmWebApp -ResourceGroupName $myResourceGroupName -Name $Resource.ResourceName
                    Set-AzureRmWebApp -ResourceGroupName $myResourceGroupName -Name $Resource.ResourceName -HttpLoggingEnabled 1 -PhpVersion 5.6
            }
        }
        Write-Output ("Completed!@Line83")
    } 
}
}

【问题讨论】:

  • 您使用的是什么部署模型? ARM 还是经典?您在订阅中的角色分配是什么?
  • @Ramana ARM 并且是所有订阅中的共同管理员角色。服务主体作为“参与者”添加到订阅中。

标签: azure powershell azure-web-app-service azure-powershell azure-automation


【解决方案1】:

希望为 自动化帐户到默认订阅可能会有所帮助

你说得对,我们可以使用 Azure 自动化账户connections来选择订阅。

例如,我们可以新建一个连接,类型为Azure service principal

像这样:

然后使用这个powershell脚本来选择连接:

$connectionName = "jason"
try
{
    # Get the connection "jason "
    $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
  }
}

这样我们就可以使用connection来选择订阅了。

顺便说一句,我们可以按照这个article来创建服务主体。

【讨论】:

  • 谢谢@Jason。是的,这部分有效,我正在使用“AzureRunAsConnection”资源,但我的挑战是发布这个.. try/catch 块后的方法 1 或 2 都无法获得订阅集 (1/2)
  • 挑战是发布 try/catch 块,我尝试获取 webapp 配置并进行一些更改。它说,没有选择订阅。如果我尝试启用订阅默认值,它会给我上面的错误,就像在主帖中一样。我不确定如何设置订阅以使用“Get-Azurewebsite”或“Set-Azurewebsite”等 cmdlet。 :(
  • @HBala 使用AzureRunAsConnection 连接,我们应该使用Get-AzureRmWebApp 来设置它,使用ARM PowerShell 命令。目前,经典网站迁移到 ARM,我们应该使用 ARM 命令来做。
  • @HBala 我们应该使用 ARM powershell 命令来执行此操作,我们只需在该运行手册中添加 powershell 脚本。当我们使用该连接时,我们将使用该订阅作为默认订阅。
  • @HBala 我们无法在那个powershell中将Azure模式切换到ASM,我们应该使用ARM命令来更改网站配置,不能使用ASM命令来更改它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-16
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
  • 1970-01-01
  • 1970-01-01
  • 2022-10-02
相关资源
最近更新 更多