I am trying to create a PowerShell function app that contains a function - a timer trigger that can start and stop another function app at a specific time.
I was motivated to try out the personal project after coming across this blog about automating start and stop time for Virtual Machine on Azure using timer trigger function.
The issue is from the Az.Functions module which I need to run a line of code
$fnapps = Get-AzFunctionApp
It runs well for the first time (No known or seen error in the logs and the output is correct). But at the second instance and other instances of running on the CRON schedule I set, I see this exception error from the module.
The error code is:
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 :
I want to know if there's something I'm missing about Az.Functions Module.
Also want to know if what I am trying to do is possible.
Note: I've confirmed the modules are installed correctly, and no issue in the other parts of the code.
Thanks.
$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" }Az.Accounts = 2.6.2module added explicitly in my requirements.psd1 in app files. Also in my function (run.ps1) I've this to get Az context and set Az-context after declaring my subscription:param($Timer)$subscriptionid = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"Set-AzContext -Subscriptionid $Subscriptionids | Out-Null`$CurrentSub = (Get-AzContext).Subscription.IdIs there anything I should set?Save-AzContext, refer this document