@符号

时间:2015-06-02 13:34:16

标签: rest powershell hashtable

我在PowerShell中做了一些REST API工作,我需要传递给REST API的一些密钥包含符号(@)。或者,从我的REST API返回的一些键包含符号(@)。

是否有转义此字符并将该值用作键的文字?

示例:

Context "Get Details By Machine Name" {
    Mock Invoke-RestMethod {
        return @{
            links = @{}
            content = @{
                @type           : CatalogResource
                id              : e5c83e8b-31df-4573-b14e-246ded62ba53
                iconId          : edd4f05a-5c59-485c-9d5b-d0280d7a6432
                resourceTypeRef : @{id=Infrastructure.Virtual; label=Virtual Machine}
            }
            metadata = @{
                size          = 20
                totalElements = 1
                totalPages    = 1
                number        = 1
                offset        = 0
            }
        }
    }

我需要@type作为哈希表的关键。

1 个答案:

答案 0 :(得分:2)

如果您的密钥名称在引号中,则以@开头的密钥名称没有问题:

PS C:\> @'
>> {
>>   "@foo" : 42
>> }
>> '@ | ConvertFrom-Json | Format-Table -AutoSize
>>

@foo
----
  42

如果您在PowerShell中定义哈希表:

PS C:\> $ht = @{ '@foo' = 42 }
PS C:\> $ht

Name                           Value
----                           -----
@foo                           42
相关问题