Azure自动化运行手册,在运行代码之前完成

问题描述 投票:0回答:1

我遇到这样的情况,在Azure自动化运行手册中,结果为“已完成”状态,而不运行“实际”代码。我已经粘贴了下面的代码。它在名称空间内创建事件中心。该代码可以在本地计算机上完美执行,但不能在Runbook中执行。

我写了一个'写输出“声明要在脚本中使用的局部变量”'->检查打印是否正常。但是,代码没有超出此范围。我敢肯定,我缺少一些东西。请帮助我。

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
    }

截屏:

enter image description here

azure-powershell azure-automation azure-runbook
1个回答
0
投票

您在Runbook的参数部分中具有$ NameSpaceNameName,但稍后在Runbook的第50行中,您具有$ NameSpaceName,这与参数部分中提到的不同。对其进行更正,然后它将按预期工作。一个建议是,无论将来有什么障碍,总是要在其他地方设置else障碍,以便将来解决此类问题。

干杯!

© www.soinside.com 2019 - 2024. All rights reserved.