异常调用";加上「;加上;2“;创建Powershell字典时的参数

异常调用";加上「;加上;2“;创建Powershell字典时的参数,powershell,Powershell,我正在尝试从以下文件hostname.csv创建字典: hostname type LON131 gaming LON132 gaming LON133 research LON134 research 使用以下Powershell脚本: get-content hostname.csv | ForEach-Object { if ($dict.Keys -contains $_.type) {

我正在尝试从以下文件hostname.csv创建字典:

hostname        type
LON131          gaming
LON132          gaming
LON133          research
LON134          research
使用以下Powershell脚本:

get-content hostname.csv | ForEach-Object {
    if ($dict.Keys -contains $_.type) {
        $dict[$_.type]+=$_.hostname
    } else {
        $dict.Add($_.type,@($_.hostname))
    }
}

Write-Host $dict;
但我一直收到以下错误消息:

Exception calling "Add" with "2" argument(s): "Key cannot be null.
Parameter name: key"
At fcheck.ps1:7 char:29
+         $dict.Add($_.type,@($_.hostname))
+                             ~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException

Exception calling "Add" with "2" argument(s): "Key cannot be null.
Parameter name: key"
At fcheck.ps1:7 char:29
+         $dict.Add($_.type,@($_.hostname))
+                             ~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException

Exception calling "Add" with "2" argument(s): "Key cannot be null.
Parameter name: key"
At fcheck.ps1:7 char:29
+         $dict.Add($_.type,@($_.hostname))
+                             ~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException

Exception calling "Add" with "2" argument(s): "Key cannot be null.
Parameter name: key"
At fcheck.ps1:7 char:29
+         $dict.Add($_.type,@($_.hostname))
+                             ~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException

Exception calling "Add" with "2" argument(s): "Key cannot be null.
Parameter name: key"
At fcheck.ps1:7 char:29
+         $dict.Add($_.type,@($_.hostname))
+                             ~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException
是什么导致了它以及如何解决它?

“密钥不能为null。”-
$。type
的计算结果与预期值不符。(返回文本行,对CSV文件/列一无所知。)

错误消息具有误导性,因为问题与参数的数量无关;这只是提供有关动态方法调用的一些详细信息的PowerShell


在任何情况下,除非出现上述问题,
Add
可以重写为
$dict[$\.type]=@($\.hostname)
,以实现统一性。当然,首先需要正确读取CSV数据。

您可以尝试将
get content hostname.CSV
替换为:

Import-CSV -Delimiter "`t" -Path hostname.csv
编辑:

我想您可能没有在初始化
dict
之前调用
$dict.keys
。以下几点对我很有用:

$dict = @{} 
Import-Csv -Path .\test.csv -Delimiter "`t" | ForEach-Object {
    if ($dict.Keys -contains $_.type) {
        $dict[$_.type]+=$_.hostname
    } else {
        $dict[$_.type] = @($_.hostname)
    }
}

$dict

@methuselah验证在每个对象的
中计算出的
$\
(和
$\.type
)值。如前所述,
$\uType
的计算结果为
null
(其中
Add
作为键被拒绝)-因此,请找出原因:)提示:
Get Content
的结果是什么?我现在得到以下错误消息
索引操作失败;数组索引的计算结果为空。在fcheck.ps1:7字符:28