[英]Exception calling “Add” with “2” argument(s) when creating Powershell dictionary
I'm trying to create a dictionary from the following file hostname.csv: 我正在尝试从以下文件hostname.csv创建字典:
hostname type
LON131 gaming
LON132 gaming
LON133 research
LON134 research
using the below Powershell script: 使用以下Powershell脚本:
get-content hostname.csv | ForEach-Object {
if ($dict.Keys -contains $_.type) {
$dict[$_.type]+=$_.hostname
} else {
$dict.Add($_.type,@($_.hostname))
}
}
Write-Host $dict;
But I keep getting the following error message: 但是我一直收到以下错误消息:
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
What could be causing it and how do I go about resolving it? 可能是什么原因导致的?如何解决?
"Key cannot be null." “键不能为空。” - $_.type
does not evaluate to what is expected. - $_.type
计算结果不符合预期。 ( Get-Content
returns lines of text and doesn't know anything about CSV files/columns.) ( Get-Content
返回文本行,对CSV文件/列一无所知。)
The error message is misleading in that the problem has nothing to do with the number of arguments; 该错误消息具有误导性,因为该问题与参数数量无关。 this is just PowerShell providing some details about the dynamic method call. 这只是PowerShell提供有关动态方法调用的一些详细信息。
In any case, barring the issue noted above, the Add
could be rewritten as $dict[$_.type] = @($_.hostname)
for uniformity. 在任何情况下,除非出现上述问题,否则为了保持一致,可以将Add
重写为$dict[$_.type] = @($_.hostname)
。 Of course the CSV data needs to be correctly read first. 当然,首先需要正确读取CSV数据。
You might try replacing get-content hostname.csv
with: 您可以尝试将get-content hostname.csv
替换为:
Import-CSV -Delimiter "`t" -Path hostname.csv
EDIT: 编辑:
I think you might not be initializing dict
priro to calling $dict.keys
. 我认为您可能不会初始化dict
priro来调用$dict.keys
。 The following worked for me: 以下为我工作:
$dict = @{}
Import-Csv -Path .\test.csv -Delimiter "`t" | ForEach-Object {
if ($dict.Keys -contains $_.type) {
$dict[$_.type]+=$_.hostname
} else {
$dict[$_.type] = @($_.hostname)
}
}
$dict
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.