【发布时间】:2022-01-19 11:52:47
【问题描述】:
我正在尝试创建一个 PowerShell 函数应用,其中包含一个函数 - 一个可以在特定时间启动和停止另一个函数应用的计时器触发器。
在遇到this blog 关于使用计时器触发功能自动启动和停止 Azure 上的虚拟机的时间后,我很有动力尝试个人项目。
问题来自我需要运行一行代码的 Az.Functions 模块
$fnapps = Get-AzFunctionApp
它第一次运行良好(日志中没有已知或看到的错误并且输出是正确的)。但是在我设置的 CRON 计划运行的第二个实例和其他实例中,我从模块中看到了这个异常错误。
错误代码是:
You cannot call a method on a null-valued expression.Exception :Type : System.Management.Automation.RuntimeExceptionErrorRecord :Exception :Type : System.Management.Automation.ParentContainsErrorRecordExceptionMessage : You cannot call a method on a null-valued expression.HResult : -2146233087CategoryInfo : InvalidOperation: (:) [], ParentContainsErrorRecordExceptionFullyQualifiedErrorId : InvokeMethodOnNullInvocationInfo :ScriptLineNumber : 450OffsetInLine : 24HistoryId : -1ScriptName :
我想知道关于 Az.Functions Module 是否缺少某些东西。
也想知道我想做的事情是否可行。
注意:我已经确认模块安装正确,代码的其他部分没有问题。
谢谢。
【问题讨论】:
-
你能分享一些代码sn-ps。异常消息表明您从 PS 访问 Fn 应用程序的方式存在问题。
-
$fnapps = Get-AzFunctionApp foreach ($fnapp in $fnapps){ if(($fnapp.Name -eq "startMe") -and ($fnapp.Status -eq "Stopped") -and ($time -gt $StartTime)){ Start-AzFunctionApp -Name $fnapp.Name -ResourceGroupName "peterRepro" Write-Host "Function App - $($fnapp.Name) Started" } -
如何在函数应用的执行过程中保留 Azure 上下文?您是否将上下文存储在 json 文件中?如果没有上下文,第二次以后,Get-AzFunctionApp 将无法检索您的 azure func 应用资源。
-
我在 app 文件的 requirements.psd1 中明确添加了
Az.Accounts = 2.6.2模块。同样在我的函数 (run.ps1) 中,我有这个来获取 Az 上下文并在声明我的订阅后设置 Az-context:param($Timer)$subscriptionid = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"Set-AzContext -Subscriptionid $Subscriptionids | Out-Null`$CurrentSub = (Get-AzContext).Subscription.Id有什么需要设置的吗? -
你必须跨会话使用上下文,所以需要使用这个
Save-AzContext,参考这个document
标签: azure powershell azure-functions azure-powershell