Azure 自动化 runbook 在 运行 代码之前完成

Azure automation runbook Completed before running the code

我有一种情况,在 Azure 自动化 运行book 中导致 'Completed' 状态,而不是 运行 'actual' 代码。我已经粘贴了下面的代码。它在命名空间内创建一个事件中心。该代码在本地机器上完美执行,但它不在 Runbook 中执行。

我写了一个 'write-output "Declaring local variables for use in script"' --> 来检查打印是否正常。但是,代码并没有超出此范围。我敢肯定,我错过了一些东西。请帮助我。

Param(
    [Parameter(Mandatory=$true)]
    [string] $NameSpaceNameName,

    [Parameter(Mandatory=$true)]
    [string[]] $EventhubNames,

    [Parameter(Mandatory=$true)]
    [string] $ProjectId,

    [Parameter(Mandatory=$true)]
    [int] $PartitionCount,

    [Parameter(Mandatory=$true)]
    [string]$Requested_for,

    [Parameter(Mandatory=$true)]
    [string]$Comments
) 

## Login to Azure using RunAsAccount
$servicePrincipalConnection = Get-AutomationConnection -Name 'AzureRunAsConnection'

Write-Output ("Logging in to Az Account...")


Login-AzAccount `
    -ServicePrincipal `
    -TenantId $servicePrincipalConnection.TenantId `
    -ApplicationId $servicePrincipalConnection.ApplicationId `
    -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 

write-output "Declaring local variables for use in script"
## Declaring local variables for use in script
$Creation_date = [System.Collections.ArrayList]@()
$ResourceGroups = Get-AzResourceGroup 
$provided_name_space_exists = $false 



## Change context to Platform subscription
select-azsubscription -subscription "GC302_Sub-platform_Dev"

## Create Event Hub
    foreach($Resourcegroup in $ResourceGroups){
        Write-Host("Processing the Resource Group: {0} " -f $Resourcegroup.ResourceGroupName) 
        $EventhubNameSpaces = Get-AzEventHubNamespace -ResourceGroupName $ResourceGroup.ResourceGroupName
        # Iterate over each Namespace. Fetch the Resource Group that contains the provided Namespace
        foreach($EHNameSpace in $EventhubNameSpaces){
            if($EHNameSpace.Name -eq $NameSpaceName){
                $provided_name_space_exists = $true
                Write-Host ("Found the provided Namespace in resource group: {0}" -f $Resourcegroup.ResourceGroupName)
                Write-Output ("Found the provided Namespace in resource group: {0}" -f $Resourcegroup.ResourceGroupName)
                $nameSpace_resource_group =  $ResourceGroup.ResourceGroupName
                # Fetch the existing Event Hubs in the Namespace
                $existing_event_hubs_list = Get-AzEventHub -Namespace $EHNameSpace.Name -ResourceGroupName $nameSpace_resource_group

                ## Check provided EH for uniqueness

                if($existing_event_hubs_list.Count -le 1000){
                       for($i = 0;$i -lt $EventhubNames.Count;$i++){
                        if($existing_event_hubs_list.name -notcontains $EventhubNames[$i]){
                            $EventHub = New-AzEventHub -ResourceGroupName $nameSpace_resource_group -Namespace $EHNameSpace.Name -Name $EventhubNames[$i] -PartitionCount $PartitionCount
                            $date = $EventHub.CreatedAt
                            $Creation_date+= $date.GetDateTimeFormats()[46]
                        }else{
                            Write-Host ("Event hub: '{0}' already exists in the NameSpace: {1}. skipping this Event hub creation" -f $EventhubNames[$i], $EHNameSpace.Name)
                        }
                    }
                }else{
                    Write-Host ("The Namespace - {0} has Event Hubs count greater or equal to 1000." -f $EHNameSpace.Name)
                    Write-Host ("Please refer the Link for Eevent Hubs quota/limit: 'https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-quotas#event-hubs-dedicated---quotas-and-limits'")
                    exit
                }
            }
        }
    }
    # Print a message that Namespace does not exist
    if($provided_name_space_exists -eq $false){
        Write-Host ("Provided NameSpace: {0} does not exist." -f $NameSpaceName)
        exit
    }

截图:

你在 Runbook 的参数部分有 $NameSpaceNameName 但稍后在 Runbook 第 50 行你有 $NameSpaceName 这是与参数部分中提到的不同。更正它,然后它应该按预期工作。一个建议是在任何有 if 块的地方始终有一个 else 块,以在将来克服此类问题。

干杯!