使用Powershell将数据添加到哈希表';找不到“的重载”;加上「;和参数计数;

使用Powershell将数据添加到哈希表';找不到“的重载”;加上「;和参数计数;,powershell,active-directory,hashtable,Powershell,Active Directory,Hashtable,我正在自学Powershell,我正在努力掌握哈希表。我明白这个概念,但应用它完全是另一回事 我使用了一篇“嘿,脚本编写人”的文章作为参考,也就是这篇文章: To create a hash table dynamically, follow these steps: 1. Create an empty hash table. 2. Store the empty hash table in a variable. 3. Collect the data.

我正在自学Powershell,我正在努力掌握哈希表。我明白这个概念,但应用它完全是另一回事

我使用了一篇“嘿,脚本编写人”的文章作为参考,也就是这篇文章:

To create a hash table dynamically, follow these steps:
1.       Create an empty hash table.
2.       Store the empty hash table in a variable.
3.       Collect the data.
4.       Store the collected data in a variable.
5.       Use the foreach statement to walk through the collected data.
6.       Inside the loop call the add method to add the key value pairs to the hash table.

An example of this procedure is shown here:
$hash = $null
$hash = @{}
$proc = get-process | Sort-Object -Property name -Unique

foreach ($p in $proc)
{
 $hash.add($p.name,$p.id)
}
这很有效,但是当我试图使其符合我的需要时,它会说:

Cannot find an overload for "Add" and the argument count: "1".
At line:15 char:10
+ $hash.add <<<< ($strGroup)
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest
我所做的只是进入一个文本文件,抓取Active Directory组的名称,返回它们(为了逐步遍历代码),目的是将列表中每个项目(文本文件)中的组的所有成员按组放入单独的数组中。例如:

Group1     Time/Date
------     ---------------
member1
member2
member3
member4

Group Owner:__________

Group2     Time/Date
------     ----------------
member1
member2
member3
member4

Group Owner:__________
问题是,我不知道我做错了什么。我通读了这篇文章,按照他的方式做得很好,但我的方式有问题。我肯定我找到了什么东西,但就是找不到


感谢您的帮助,欢迎您的批评。

每个哈希表条目都包含一个键和一个值。
在这里:

您正在向哈希表中添加一个新条目,该条目的键为$p.name中的任意项,值为$p.id中的任意项。提供的.add方法有两个参数-一个用于键,另一个用于值

在这里:

您只提供了一个参数,而该方法不知道如何处理它-它需要一个键和值的参数,但没有足够的参数可以使用

编辑:

我认为这可能更接近你想要实现的目标:

import-module activedirectory
$strDate = Get-Date
$strGroupList = get-content "C:\Users\MYUSERNAME\Documents\Group Audit\audit.txt"


$strGroupList #list groups found

$hash= $null #empty the hash table
$hash = @{} 


foreach ($strGroup in $strGroupList)
{
$strGroupDetails = Get-ADGroupMember -identity $strGroup 
$hash.add($strGroup,$strGroupDetails)
}

如果您确实想等待第二次传递来添加值,您可以首先将它们设置为
$null
,例如:
$hash.add($strGroup,$null)
$hash.add($p.name,$p.id)
$hash.add($strGroup)
import-module activedirectory
$strDate = Get-Date
$strGroupList = get-content "C:\Users\MYUSERNAME\Documents\Group Audit\audit.txt"


$strGroupList #list groups found

$hash= $null #empty the hash table
$hash = @{} 


foreach ($strGroup in $strGroupList)
{
$strGroupDetails = Get-ADGroupMember -identity $strGroup 
$hash.add($strGroup,$strGroupDetails)
}