question

MulderSidney-6129 avatar image
0 Votes"
MulderSidney-6129 asked ryanchill edited

Get-AzWebApp return null

Has anything changed on the endpoints handling get-azwebapp requests?

I have a Azure DevOps pipeline running several tasks. one of which grants access to a keyvault for WebApps matching a particular Tag. Let say: Tag:"Env" with Value:"Dev".

I used to run this code:

 $webapps = Get-AzWebApp | where {$_.Tags.Item('env') -match "dev"}
 foreach($webapp in $webapps){
     $faMid = $webapp.Identity.PrincipalId
     Set-AzKeyVaultAccessPolicy -VaultName $(kvName) -ObjectId $faMid -PermissionsToSecrets "get","list"
 }

In a step in azure devops. $(kvName) is a devops variable containing the name of the keyvault I want to update.
For testing purposes I change this to a sting containing the name of the keyvault.

When I run

 $webapps = Get-AzWebApp | where {$_.Tags.Item('env') -match "dev"} I get an error:
 You cannot call a method on a null-valued expression.
 At line:1 char:34
 + ...  = Get-AzWebApp | where {$_.Tags.Item('env') -eq "dev"}
 +                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
     + FullyQualifiedErrorId : InvokeMethodOnNull

But when I run Get-AzWebApp | where {$_.Tags.Item('env') -match "dev"}
a result set containing the webapps I need is return perfectly.

Just putting $webapps = in front of the working code breaks it.
I have tried this on my own development machine with the latest az cmdlets and on our deployment server.
Both fail.

I ran this pipeline just last week without any issue but today I failed without any change to the code.

Any thoughts?




Using Powershell 5.1 and Az Module 5.6.0

windows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @MulderSidney

I'm able to reproduce the same issue you're seeing, in both PowerShell and PowerShell Core. The latest Az module I have is 4.6.1 on PowerShell 5.1 and PowerShell Core 7.1.2 . I personally have an update for Visual C++ 2010 x86/x64 redistributable that was installed on 3/3/2021. Not sure if that update has anything to do with it, but this issue should be reported to the PowerShell team.

@MichaelHan-MSFT, do you have any thoughts on this?

Regards,
Ryan

1 Vote 1 ·
MulderSidney-6129 avatar image
0 Votes"
MulderSidney-6129 answered MulderSidney-6129 commented

Hi @ryanchill,

Thanks for confirming that i'm not crazy. Was doubting myself for a moment.

I have made a new observation. When executing:

 Get-AzWebApp | where {$_.Tags.Item('env') -match "dev"}

I do also get a null value error in the return. Didn't see it before because a lot of data is being dumped to the screen.
So I created a bit of code to loop the entire result and dumping it one by one.

 Get-AzWebApp | where {$_.Tags.Item('env') -match "dev"} | % {
     $name = $_.Name
     write-host $name -ForegroundColor Green
     Get-AzWebApp -Name $name | where {$_.Tags.Item('env') -match "dev"}
 }

This dumps all the webapps but also provides me the exact app generating the error.
Just after the name of an older app we are running the error is also generated. Not sure why yet.

When I execute :

 Get-AzWebApp -Name <name older app> | where {$_.Tags.Item('env') -match "dev"}


the error is not displayed.

I'm looking further and will keep this post up-to-date.








· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Still not sure why this issue popped up now but I have found a solution/workaround.
After looping all the webapps I found one that didn't have any tags.
This caused the null valued error generated by the $_.Tags.Item('env') part in the where clause.
After changing the where clause to the following this issue was fixed and the rest of the code could continue.

 $webapps = Get-AzWebApp | where {$_.Tags -ne $null -and $_.Tags.Item('env') -match "dev"}

I'd like to emphasize that this code worked fine the last time the pipeline ran on march 3rd but somehow isn't working anymore.
If anybody can explain this I'd appreciate it.

For now I can continue redeploying the resources again.



0 Votes 0 ·
ryanchill avatar image
0 Votes"
ryanchill answered ryanchill edited

@MulderSidney-6129, I found that by doing

 $webapps = Get-AzWebApp | Where-Object {$_.Tags["mytag"] -match "myvalue"}

doesn't result in the InvalidOperation error. My guess is that something changed with the handling of null valued IDictionary<T,T>. I agree with adding $_.Tags -ne null to avoid messages like InvalidOperation: Cannot index into a null array. but it didn't affect $webapps from containing the values.

Regards,
Ryan


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.