带有 powershell 的 Azure 函数应用程序无法启动容器实例

Azure function app with powershell cannot start container instances

有一个资源组有两种重要的资源类型:第一种是容器实例 (Linux)。第二个是功能应用程序(Powershell,Windows)。后者的函数应该启动前者,但它会抛出错误:Cannot bind argument to parameter 'InputObject' because it is null

我想念什么? this question 的答案仅在 运行 在 Function App、Azure 或 VSCode 中使用 Function Tools 时给出相同的错误。当 运行 在终端中时,命令 Invoke-AzResourceAction 正常工作。

功能代码如下:

# Input bindings are passed in via param block.
param($Timer)

# Get the current universal time in the default string format
$currentUTCtime = (Get-Date).ToUniversalTime()

# The 'IsPastDue' porperty is 'true' when the current function invocation is later than scheduled.
if ($Timer.IsPastDue) {
    Write-Host "PowerShell timer is running late!"
}

# Write an information log with the current time.
Write-Host "PowerShell timer trigger function ran! TIME: $currentUTCtime"

$cg = Get-AzContainerGroup -ResourceGroupName some-group-name -Name container-instance-name

Invoke-AzResourceAction -ResourceId $cg.Id `
    -Action start `
    -Force

这是输出日志:

Executing task: func host start

Azure Functions Core Tools Core Tools Version: 3.0.2996 Commit hash: c54cdc36323e9543ba11fb61dd107616e9022bba Function Runtime Version: 3.0.14916.0

Functions:

(function name): timerTrigger

For detailed output, run func with --verbose flag. Executing 'Functions.' (Reason='Timer fired at 2020-12-04T13:11:01.1496065+02:00', Id=c0b30df8-065d-4725-8fa7-22fdfad71a21) Trigger Details: UnscheduledInvocationReason: IsPastDue, OriginalSchedule: 2020-12-04T13:11:00.0000000+02:00 Worker process started and initialized. INFORMATION: PowerShell timer is running late! INFORMATION: PowerShell timer trigger function ran! TIME: 12/04/2020 11:11:06 INFORMATION: /subscriptions//resourceGroups//providers/Microsoft.ContainerInstance/containerGroups/ ERROR: Cannot bind argument to parameter 'InputObject' because it is null.

Exception : Type : System.Management.Automation.ParameterBindingValidationException
Message : Cannot bind argument to parameter 'InputObject' because it is null. ParameterName : InputObject ParameterType : System.Object ErrorId : ParameterArgumentValidationErrorNullNotAllowed CommandInvocation : MyCommand : Trace-PipelineObject HistoryId : 1 InvocationName : Microsoft.Azure.Functions.PowerShellWorker\Trace-PipelineObject PipelineLength : 2 PipelinePosition : 2 ExpectingInput : True ErrorRecord : Exception : Type : System.Management.Automation.ParentContainsErrorRecordException
Message : Cannot bind argument to parameter 'InputObject' because it is null. HResult : -2146233087 CategoryInfo : InvalidData: (:) [Trace-PipelineObject], ParentContainsErrorRecordException FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Azure.Functions.PowerShellWorker.Commands.TracePipelineObjectCommand InvocationInfo : MyCommand : Trace-PipelineObject HistoryId : 1 CommandOrigin : Internal ScriptStackTrace : at , \orchestration<function name>\run.ps1: line 19
TargetSite : Name : BindValueFromPipeline DeclaringType : System.Management.Automation.CmdletParameterBinderController, System.Management.Automation, Version=7.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 MemberType : Method Module : System.Management.Automation.dll StackTrace : at System.Management.Automation.CmdletParameterBinderController.BindValueFromPipeline(PSObject inputToOperateOn, MergedCompiledCommandParameter parameter, ParameterBindingFlags flags) at System.Management.Automation.CmdletParameterBinderController.BindUnboundParametersForBindingStateInParameterSet(PSObject inputToOperateOn, CurrentlyBinding currentlyBinding, UInt32 validParameterSets) at System.Management.Automation.CmdletParameterBinderController.BindUnboundParametersForBindingState(PSObject inputToOperateOn, CurrentlyBinding currentlyBinding, UInt32 validParameterSets) at System.Management.Automation.CmdletParameterBinderController.BindPipelineParametersPrivate(PSObject inputToOperateOn) at System.Management.Automation.CmdletParameterBinderController.BindPipelineParameters(PSObject inputToOperateOn) at System.Management.Automation.CommandProcessor.Read() Source : System.Management.Automation HResult : -2146233087 CategoryInfo : InvalidData: (:) [Trace-PipelineObject], ParameterBindingValidationException FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Azure.Functions.PowerShellWorker.Commands.TracePipelineObjectCommand InvocationInfo : MyCommand : Trace-PipelineObject HistoryId : 1 CommandOrigin : Internal ScriptStackTrace : at , \orchestration<function name>\run.ps1: line 19

Result: ERROR: Cannot bind argument to parameter 'InputObject' because it is null.

Exception : Type : System.Management.Automation.ParameterBindingValidationException Message : Cannot bind argument to parameter 'InputObject' because it is null. ParameterName : InputObject ParameterType : System.Object ErrorId : ParameterArgumentValidationErrorNullNotAllowed CommandInvocation : MyCommand : Trace-PipelineObject HistoryId : 1 InvocationName : Microsoft.Azure.Functions.PowerShellWorker\Trace-PipelineObject PipelineLength : 2 PipelinePosition : 2 ExpectingInput : True ErrorRecord : Exception : Type : System.Management.Automation.ParentContainsErrorRecordException Message : Cannot bind argument to parameter 'InputObject' because it is null. HResult : -2146233087 CategoryInfo : InvalidData: (:) [Trace-PipelineObject], ParentContainsErrorRecordException FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Azure.Functions.PowerShellWorker.Commands.TracePipelineObjectCommand InvocationInfo : MyCommand : Trace-PipelineObject HistoryId : 1 CommandOrigin : Internal ScriptStackTrace : at , <function name>\run.ps1: line 19
TargetSite : Name : BindValueFromPipeline DeclaringType : System.Management.Automation.CmdletParameterBinderController, System.Management.Automation, Version=7.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 MemberType : Method Module : System.Management.Automation.dll StackTrace : at System.Management.Automation.CmdletParameterBinderController.BindValueFromPipeline(PSObject inputToOperateOn, MergedCompiledCommandParameter parameter, ParameterBindingFlags flags) at System.Management.Automation.CmdletParameterBinderController.BindUnboundParametersForBindingStateInParameterSet(PSObject inputToOperateOn, CurrentlyBinding currentlyBinding, UInt32 validParameterSets) at System.Management.Automation.CmdletParameterBinderController.BindUnboundParametersForBindingState(PSObject inputToOperateOn, CurrentlyBinding currentlyBinding, UInt32 validParameterSets) at System.Management.Automation.CmdletParameterBinderController.BindPipelineParametersPrivate(PSObject inputToOperateOn) at System.Management.Automation.CmdletParameterBinderController.BindPipelineParameters(PSObject inputToOperateOn) at System.Management.Automation.CommandProcessor.Read() Source : System.Management.Automation HResult : -2146233087 CategoryInfo : InvalidData: (:) [Trace-PipelineObject], ParameterBindingValidationException FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Azure.Functions.PowerShellWorker.Commands.TracePipelineObjectCommand InvocationInfo : MyCommand : Trace-PipelineObject HistoryId : 1 CommandOrigin : Internal ScriptStackTrace : at , \orchestration<function name>\run.ps1: line 19 Exception: Cannot bind argument to parameter 'InputObject' because it is null. Stack: at System.Management.Automation.CmdletParameterBinderController.BindValueFromPipeline(PSObject inputToOperateOn, MergedCompiledCommandParameter parameter, ParameterBindingFlags flags) at System.Management.Automation.CmdletParameterBinderController.BindUnboundParametersForBindingStateInParameterSet(PSObject inputToOperateOn, CurrentlyBinding currentlyBinding, UInt32 validParameterSets) at System.Management.Automation.CmdletParameterBinderController.BindUnboundParametersForBindingState(PSObject inputToOperateOn, CurrentlyBinding currentlyBinding, UInt32 validParameterSets) at System.Management.Automation.CmdletParameterBinderController.BindPipelineParametersPrivate(PSObject inputToOperateOn) at System.Management.Automation.CmdletParameterBinderController.BindPipelineParameters(PSObject inputToOperateOn) at System.Management.Automation.CommandProcessor.Read(). Executed 'Functions.' (Succeeded, Id=c0b30df8-065d-4725-8fa7-22fdfad71a21, Duration=13327ms) Host lock lease acquired by instance ID '0000000000000000000000008ABBCF1E'.

显然,您脚本中的命令之一输出 $null,它会遇到此错误:https://github.com/Azure/azure-functions-powershell-worker/issues/561。最有可能的是,Invoke-AzResourceAction 会这样做。解决方法是重定向或捕获输出,例如:

Invoke-AzResourceAction -ResourceId $cg.Id `
    -Action start `
    -Force | Out-Null

该错误最近已得到修复,但需要几周时间才能将修复程序部署到 Azure。