使用New-AzAutomationSchedule时遭遇空引用异常的技术求助
Azure自动化Runbook调度作业时持续触发NullReferenceException
问题概述
在Azure自动化Runbook中执行New-AzAutomationSchedule和Register-AzAutomationScheduledRunbook cmdlet时,无论传入参数是否合法(包括硬编码参数),均触发错误:Object reference not set to an instance of an object。
环境信息
- 主要使用PowerShell 5.1运行时(因依赖SqlServer模块的
Invoke-ProcessASDatabase方法,仅该版本支持) - 已测试PowerShell 7.1、7.2运行时,问题同样存在
排查尝试
为排除变量取值问题,尝试硬编码合法参数调用:
New-AzAutomationSchedule -AutomationAccountName "account1" -ResourceGroupName "resource1" -Name "Test-Daily-Schedule" -StartTime "23:00" -DayInterval 1 Register-AzAutomationScheduledRunbook -AutomationAccountName "account1" -ResourceGroupName "resource1" -ScheduleName "Test-Daily-Schedule" -RunbookName "runbook1"
完整错误信息
New-AzAutomationSchedule : Object reference not set to an instance of an object. At line:122 char:17 + ... New-AzAutomationSchedule -AutomationAccountName $Automati ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [New-AzAutomationSchedule], NullReferenceException + FullyQualifiedErrorId : Microsoft.Azure.Commands.Automation.Cmdlet.NewAzureAutomationSchedule
测试代码
$Credential = Get-AutomationPSCredential -Name "ps_name" Connect-AzAccount -Credential $Credential Set-AzContext -SubscriptionId "***" $AutomationAccountName = "account1" $ResourceGroupName = "resource1" $StartTime = "23:00" $TimeZone = ([System.TimeZoneInfo]::Local).Id $context = Get-AzContext Write-Output "Scheduling job" Try { New-AzAutomationSchedule -AutomationAccountName $AutomationAccountName -ResourceGroupName $ResourceGroupName -Name "Schedule01" -StartTime $StartTime -TimeZone $TimeZone -OneTime -DefaultProfile $context } Catch { Write-Error $_.Exception.Message } Write-Output "Registering runbook" Try { Register-AzAutomationScheduledRunbook -AutomationAccountName $AutomationAccountName -ResourceGroupName $ResourceGroupName -ScheduleName "Schedule01" -RunbookName "runbook1" } Catch { Write-Error $_.Exception.Message }
解决思路
- 检查Az模块版本:NullReferenceException常出现在模块版本不兼容或存在bug的场景。确认自动化账户中Az.Automation模块的版本,尝试升级至最新稳定版,或回退到已知可用的旧版本(如2.0.0)。
- 修正StartTime参数格式:
StartTime需要包含完整的日期和时间,仅传入23:00会导致解析异常。改为Get-Date -Hour 23 -Minute 0 -Second 0生成完整DateTime对象,或使用"YYYY-MM-DD 23:00"格式的字符串。 - 验证时区参数有效性:
([System.TimeZoneInfo]::Local).Id在自动化环境中可能返回非标准时区ID,建议使用明确的时区名称(如"China Standard Time"),避免依赖本地时区。 - 移除DefaultProfile参数:在Runbook中使用
Connect-AzAccount后,无需手动指定-DefaultProfile $context,模块会自动使用当前上下文,多余的参数可能导致内部对象引用错误。 - 检查权限配置:确认Runbook使用的服务主体拥有自动化账户的
Automation Contributor或更高权限,权限不足可能导致内部对象初始化失败。
修改后的测试代码示例:
$Credential = Get-AutomationPSCredential -Name "ps_name" Connect-AzAccount -Credential $Credential Set-AzContext -SubscriptionId "***" $AutomationAccountName = "account1" $ResourceGroupName = "resource1" # 使用完整DateTime对象 $StartTime = Get-Date -Hour 23 -Minute 0 -Second 0 # 指定明确时区 $TimeZone = "China Standard Time" Write-Output "Scheduling job" Try { New-AzAutomationSchedule -AutomationAccountName $AutomationAccountName ` -ResourceGroupName $ResourceGroupName ` -Name "Schedule01" ` -StartTime $StartTime ` -TimeZone $TimeZone ` -OneTime } Catch { Write-Error $_.Exception.Message Write-Error $_.ScriptStackTrace } Write-Output "Registering runbook" Try { Register-AzAutomationScheduledRunbook -AutomationAccountName $AutomationAccountName ` -ResourceGroupName $ResourceGroupName ` -ScheduleName "Schedule01" ` -RunbookName "runbook1" } Catch { Write-Error $_.Exception.Message Write-Error $_.ScriptStackTrace }
内容的提问来源于stack exchange,提问作者Curt C.
相关产品推荐
相关产品推荐

