0

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.

7
  • can you share some code snippets. the exception message indicates there is some issue in the way you are accessing the Fn app from PS. Commented Dec 16, 2021 at 12:58
  • $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" } Commented Dec 16, 2021 at 14:06
  • How are you retaining your Azure context across the executions of your function app? Do you store the context in json file? Without context, the second time onwards, the Get-AzFunctionApp will not be able to retrieve your azure func app resources. Commented Dec 16, 2021 at 14:35
  • I have the Az.Accounts = 2.6.2 module 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.Id Is there anything I should set? Commented Dec 16, 2021 at 14:51
  • You have to use the context across sessions, so need to use this Save-AzContext , refer this document Commented Dec 16, 2021 at 16:42

1 Answer 1

1

Thankyou Anand-Sowmithiran. Posting your suggestion as an answer so that it will be helpful for other community members who face similar kind of issues.

As per the document you need to use the context across sessions

Below is the sample Save-AzContext syntax

Save-AzContext
    [[-Profile] <AzureRmProfile>]
    [-Path] <String>
    [-Force]
    [-DefaultProfile <IAzureContextContainer>]
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

for further information check the az.accounts module

Sign up to request clarification or add additional context in comments.

1 Comment

@AnandSowmithiran I'm so grateful for the help thus far. I want to know where to use this Save-AzContext? Is it in my profile.ps1 if ($env:MSI_SECRET){ Connect-AzAccount -Identity Save-AzContext -Profile } Or in the code itself (run.ps1). Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.