我正在尝试从以下文件 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
可能是什么原因造成的,我该如何解决?