哈希文字中不允许使用空键

A null key is not allowed in a hash literal

我希望能够将这个哈希表保存在一个名为:$arr 的变量中。

如您所见,我们定义了两个变量 $displayName 和 $typekind,我将它们放在哈希表中。

但是当我尝试 运行 脚本时,它给了我一个错误:

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

我尝试了一些不同的方法,但我不确定错误发生在哪里。希望你能帮助我

...

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 
        }

PowerShell 正在尝试将 $schema(变量)的值 分配为 [=21] 的 Key =]散列table。看起来并且可能是对您的错误的唯一可能解释,该变量尚未定义因此实际上是 $null:

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

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

如果您想将 $schema 指定为 Key 的字面名称,您可以使用 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