Home > Software design >  A null key is not allowed in a hash literal
A null key is not allowed in a hash literal

Time:03-21

I would like to be able to save this hashtable in a variable called: $arr.

As you can see we define two variables $displayName and $typekind and I put them in the hashtable.

But when i try to run the script it gives me an error:

InvalidOperation: A null key is not allowed in a hash literal.

I have tried some different things but i am not sure where error is happening. Hope you can help me

...

if (!( $resourceTypes -like ("*"   $resource.type   "*")) -and ($azDiagSetting)) {
            $displayName = $resource.type.replace('/', ' ').Split(' ')[-1]
            $typekind = $resource.type
            $arr = @{
                type       = "Microsoft.Authorization/policyDefinitions"
                apiVersion = "2020-09-01"
                properties = @{
                    metadata    = @{
                        category = "Monitoring"
                    }
                    description = "This policy automatically deploys and enable diagnostic settings to Log Analytics"
                    displayName = "Apply diagnostic settings for $($displayName) - Log Analytics" 
                    policyRule  = @{
                        then = @{
                            details = @{
                                roleDefinitionIds  = @(
                                    "/providers/Microsoft.Authorization/roleDefinitions/92aaf0da-9dab-42b6-94a3-d43ce8d16293"
                                )
                                name               = "setByPolicy"
                                type               = "Microsoft.Insights/diagnosticSettings"
                                existenceCondition = @{
                                    AllOf = @(
                                        @{
                                            matchInsensitively = "[[parameters('logAnalytics')]"
                                            field              = "Microsoft.Insights/diagnosticSettings/workspaceId"
                                        }
                                    )
                                }
                                deployment         = @{
                                    properties = @{
                                        template   = @{
                                            contentVersion = "1.0.0.0"
                                            resources      = @(
                                                @{
                                                    type       = "$($typekind)/providers/diagnosticSettings"
                                                    apiVersion = "2021-05-01-preview"
                                                    properties = @{
                                                        metrics     = @(
                                                            @{ }
                                                        )
                                                        workspaceId = "[[parameters('logAnalytics')]"
                                                        logs        = @(
                                                            @{}
                                                        )
                                                    }
                                                    location   = "[[parameters('location')]"
                                                    name       = "[[concat(parameters('resourceName'), '/', 'Microsoft.Insights/setByPolicy')]"
                                                }
                                            )
                                            parameters     = @{
                                                location     = @{
                                                    type = "string"
                                                }
                                                logAnalytics = @{
                                                    type = "string"
                                                }
                                                resourceName = @{
                                                    type = "string"
                                                }
                                            }
                                            $schema        = "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#"
                                        }
                                        parameters = @{
                                            location     = @{
                                                value = "[[field('location')]"
                                            }
                                            logAnalytics = @{
                                                value = "[[parameters('logAnalytics')]"
                                            }
                                            resourceName = @{
                                                value = "[[field('name')]"
                                            }
                                        }
                                        mode       = "incremental"
                                    }
                                }
                            }
                        }
                    }
                }
            } | ConvertTo-Json -Depth 20
            $arr 
        }

CodePudding user response:

PowerShell is attempting to assign the value of $schema (a variable) as a Key of your hash table. As it seems and could be the only possible explanation for your error, this variable hasn't been defined hence is effectively $null:

$schema = $null
$hash = @{
    $schema = 'hello'
}

# Above errors with the exception message:
# "A null key is not allowed in a hash literal."

If you want to assign $schema as the literal name of your Key, you can use single-quoted strings:

A string enclosed in single-quotation marks is a verbatim string. The string is passed to the command exactly as you type it. No substitution is performed.

$hash = @{
    '$schema' = 'hello'
}

# Results in:

Name                           Value
----                           -----
$schema                        hello
  • Related