Get-AzWebApp return null

Sidney Mulder 26 Reputation points

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,448 questions
{count} vote

2 answers

Sort by: Most helpful
  1. Sidney Mulder 26 Reputation points

    Hi @Ryan Hill ,

    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.

  2. Ryan Hill 26,946 Reputation points Microsoft Employee

    @Anonymous , 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.


    0 comments No comments