首页 > 解决方案 > 带有 powershell 的 Azure 函数应用无法启动容器实例

问题描述

有一个具有两种重要资源类型的资源组:第一种是容器实例 (Linux)。第二个是函数应用程序(Powershell、Windows)。后者的函数应该启动前者,但它会引发错误:无法将参数绑定到参数“InputObject”,因为它为空

我想念什么?仅当在 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

这是输出日志:

执行任务:func host start

Azure Functions 核心工具核心工具版本:3.0.2996 提交哈希:c54cdc36323e9543ba11fb61dd107616e9022bba 函数运行时版本:3.0.14916.0

功能:

(函数名):timerTrigger

有关详细输出,请使用 --verbose 标志运行 func。执行“功能”。(原因='计时器在 2020-12-04T13:11:01.1496065+02:00',Id=c0b30df8-065d-4725-8fa7-22fdfad71a21)触发详细信息:UnscheduledInvocationReason:IsPastDue,OriginalSchedule:2020-12-04T13:11 :00.0000000+02:00 工作进程启动并初始化。信息:PowerShell 计时器运行迟了!信息:PowerShell 计时器触发功能已运行!时间:12/04/2020 11:11:06 信息:/subscriptions//resourceGroups//providers/Microsoft.ContainerInstance/containerGroups/ 错误:无法将参数绑定到参数“InputObject”,因为它为空。

异常:类型:System.Management.Automation.ParameterBindingValidationException
消息:无法将参数绑定到参数“InputObject”,因为它为空。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:异常:类型:系统.Management.Automation.ParentContainsErrorRecordException
消息:无法将参数绑定到参数“InputObject”,因为它为空。HResult:-2146233087 CategoryInfo:InvalidData:(:) [Trace-PipelineObject],ParentContainsErrorRecordExceptionfullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Azure.Functions.PowerShellWorker.Commands.TracePipelineObjectCommand InvocationInfo:MyCommand:Trace-PipelineObject HistoryId:1 CommandOrigin:内部ScriptStackTrace:在\orchestration<函数名称>\run.ps1:第 19 行
TargetSite:名称:BindValueFromPipeline DeclaringType:System.Management.Automation.CmdletParameterBinderController,System.Management.Automation,版本=7.0.3.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35 MemberType:方法模块:System.Management.Automation.dll StackTrace:在系统System.Management.Automation.CmdletParameterBinderController.BindUnboundParametersForBindingStateInParameterSet(PSObject inputToOperateOn,CurrentlyBinding currentBinding,UInt32 validParameterSets)在 System.Management.Automation.CmdletParameterBinderController.BindUnboundParametersForBindingStateInParameterSet(PSObject inputToOperateOn,UInt32 validParameterSets)在 System.Management.Automation.CmdletParameterBinderController.BindValueFromPipeline(PSObject inputToOperateOn,MergedCompiledCommandParameter 参数,ParameterBindingFlags 标志)。 inputToOperateOn,CurrentBinding currentBinding, UInt32 validParameterSets) at System.Management.Automation.CmdletParameterBinderController.BindPipelineParametersPrivate(PSObject inputToOperateOn) at System.Management.Automation.CmdletParameterBinderController.BindPipelineParameters(PSObject inputToOperateOn) at System.Management.Automation.CommandProcessor.Read() 来源:系统。 Management.Automation HResult:-2146233087 CategoryInfo:InvalidData:(:) [Trace-PipelineObject],ParameterBindingValidationExceptionfullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Azure.Functions.PowerShellWorker.Commands.TracePipelineObjectCommand InvocationInfo:MyCommand:Trace-PipelineObject HistoryId:1 CommandOrigin:内部ScriptStackTrace : 在 , \编排<函数名>\run.ps1:第 19 行

结果:错误:无法将参数绑定到参数“InputObject”,因为它为空。

异常:类型:System.Management.Automation.ParameterBindingValidationException 消息:无法将参数绑定到参数“InputObject”,因为它为空。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:异常:类型:系统.Management.Automation.ParentContainsErrorRecordException 消息:无法将参数绑定到参数“InputObject”,因为它为空。HResult : -2146233087 CategoryInfo : InvalidData: (:) [Trace-PipelineObject],
函数名称>\run.ps1:第 19 行异常:无法将参数绑定到参数“InputObject”,因为它为空。堆栈:在 System.Management.Automation.CmdletParameterBinderController.BindUnboundParametersForBindingStateInParameterSet(PSObject inputToOperateOn,CurrentlyBinding currentBinding,UInt32 validParameterSets) 在 System.Management.Automation.CmdletParameterBinderController. System.Management.Automation.CmdletParameterBinderController.BindPipelineParametersPrivate(PSObject inputToOperateOn) 中的 .BindUnboundParametersForBindingState(PSObject inputToOperateOn, CurrentBinding currentBinding, UInt32 validParameterSets)。System.Management.Automation.CommandProcessor.Read() 中的 Management.Automation.CmdletParameterBinderController.BindPipelineParameters(PSObject inputToOperateOn)。执行“功能”。(成功,Id=c0b30df8-065d-4725-8fa7-22fdfad71a21,持续时间=13327ms)实例 ID '0000000000000000000000008ABBCF1E' 获取的主机锁租约。

标签: powershellazure-functionsazure-container-instances

解决方案


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

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

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


推荐阅读