Search code examples
azure-powershellazure-automationazure-runbook

Azure automation runbook Completed before running the code


I have a situation where in the Azure automation runbook results in 'Completed' state and does not run the 'actual' code. I have pasted the code below. It creates a Event Hub inside a Namespace. The code works perfectly executing in local machine but it does not execute in Runbook.

I have written a 'write-output "Declaring local variables for use in script"' --> to check if the printing is working. However, the code is not going beyond that. I am sure, I am missing some thing. Kindly help me.

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://learn.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
    }

Screenshot:

enter image description here


Solution

  • You have $NameSpaceNameName in the parameters section of the runbook but later in the runbook at 50th line you have $NameSpaceName which is not the same as mentioned in parameters section. Correct it and then it should work as expected. One suggestion is to always have an else block wherever you have if block to overcome such issues in future.