【问题标题】:Why am I getting this Powershell hash literal was incomplete error?为什么我收到此 Powershell 哈希文字不完整错误?
【发布时间】:2021-02-28 21:33:04
【问题描述】:

我正在尝试在 PowerShell 中设置哈希表(字典)。当我尝试这条线时:

$users = @{abertram = 'Adam Bertram' raquelcer = 'Raquel Cerillo' zheng21 = 'Justin Zheng'}

我收到以下错误:

At line:1 char:38
+ $users = @{abertram = 'Adam Bertram' raquelcer = 'Raquel Cerillo' zhe ...
+                                      ~~~~~~~~~
Unexpected token 'raquelcer' in expression or statement.
At line:1 char:37
+ $users = @{abertram = 'Adam Bertram' raquelcer = 'Raquel Cerillo' zhe ...
+                                     ~
The hash literal was incomplete.
At line:1 char:91
+ ... 'Adam Bertram' raquelcer = 'Raquel Cerillo' zheng21 = 'Justin Zheng'}
+                                                                         ~
Unexpected token '}' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

为什么我会得到这个以及如何在 PowerShell 中正确设置哈希表?

【问题讨论】:

    标签: powershell hashtable


    【解决方案1】:

    您必须在同一行中使用分号 ; 分隔键/值对

    $users = @{abertram = 'Adam Bertram'; raquelcer = 'Raquel Cerillo'; zheng21 = 'Justin Zheng'}
    

    不过,最好使用新行:

    $users =
    @{
        abertram = 'Adam Bertram'
        raquelcer = 'Raquel Cerillo'
        zheng21 = 'Justin Zheng'
    }
    

    【讨论】:

    • 是的!做到了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 2018-06-22
    • 2016-01-01
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多