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

我遇到的情况是,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
    }

屏幕截图:

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

zhangwen158 回答:Azure自动化运行手册在运行代码之前完成

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

干杯!

本文链接:https://www.f2er.com/2520239.html

大家都在问