简体   繁体   English

创建Powershell字典时,异常调用带有“ 2”参数的“添加”

[英]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.

相关问题 Powershell:使用“ 3”参数调用“ AttachDatabase”的异常 - Powershell: Exception calling “AttachDatabase” with “3” argument(s) PowerShell:使用“7”参数调用“GetListItems”异常 - PowerShell: Exception calling “GetListItems” with “7” argument(s) 使用“1”参数调用“SetAccessRule”的异常::: Powershell 错误 - Exception calling "SetAccessRule" with "1" argument(s) ::: Powershell Error Powershell Invoke-SSHCommand:使用“1”参数调用“EndExecute”的异常 - Powershell Invoke-SSHCommand: Exception calling “EndExecute” with “1” argument(s) PowerShell 异常从另一个函数中使用“0”参数调用“Start” - PowerShell Exception calling “Start” with “0” argument(s) from within another function Powershell 将文件上传到 SharePoint:异常调用带有“0”参数的“ExecuteQuery”错误 - Powershell uploading files to SharePoint : Exception calling “ExecuteQuery” with “0” argument(s) error 如何调试使用“5”参数调用“InvokeMember”的powershell异常:类型不匹配 - How to debug a powershell exception calling "InvokeMember" with "5" argument(s): Type mismatch Powershell 错误:使用“2”参数调用“批准”的异常:值不能是 null - Powershell Error: Exception Calling “Approve” with “2” argument(s): Value cannot be null powershell:使用“1”参数调用“填充”的异常:“'/'附近的语法不正确。” ' - powershell: Exception calling “Fill” with “1” argument(s): “Incorrect syntax near '/'.” ' 使用“1”参数调用“发送”的 PowerShell 异常:“发送邮件失败”。 - PowerShell Exception calling “Send” with “1” argument(s): “Failure sending mail.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM