Automation Runbook无法在运行

时间:2018-01-03 08:49:59

标签: azure powershell azure-web-sites azure-powershell azure-automation

克隆样本非经典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,必须设置订阅。

注意:希望将自动化帐户中使用的Service Principal添加到默认订阅可能有所帮助,我还尝试将ApplicationId添加到Subscription的Access控件作为贡献者。虽然我在这里提到的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")
    } 
}
}

1 个答案:

答案 0 :(得分:1)

  

希望为。添加serviceprincipal类型添加   automationaccount到默认订阅可能有帮助

您说得对,我们可以使用Azure自动化帐户连接来选择订阅。

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

像这样:
enter image description here

enter image description here

然后使用此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
  }
}

通过这种方式,我们可以使用连接来选择订阅。

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