键和值的哈希集

Hashset to keys and values

我的脚本有问题:

这里我设置了一个var remotefilehash:

$remotefilehash = $remotefiles -replace '^[a-f0-9]{32}(  )', '[=10=]=  ' | ConvertFrom-StringData

这将创建一个哈希表

然后我将这些哈希与我在本地的哈希集中的哈希进行比较

# Create a hash set from the local hashes.

$localHashSet = [System.Collections.Generic.HashSet[string]] $localmd5

# Loop over all remote hashes to find those not among the local hashes.
$diffmd5 = $remotefilehash.Keys.Where({ -not $localHashSet.Contains($_) })

这为我提供了散列键,但我随后还需要获取在上面找到的那些键的散列值...

this creates a hashtable

实际上,它创建了一个 array of hashtables, because ConvertFrom-StringData每个管道输入对象 变成一个单独的哈希表。

要创建一个 single 哈希表,首先将输入行连接起来形成一个 single 字符串:

$remotefilehash = 
  ($remotefiles -replace '^[a-f0-9]{32}(  )', '[=10=]=  ' -join "`n") |
     ConvertFrom-StringData

枚举哈希表的键值对而不只是 (.Keys)(-like ) 对象,你需要使用它的 .GetEnumerator() 方法:

$diffmd5 = 
  $remotefilehash.GetEnumerator().Where({ -not $localHashSet.Contains($_.Key) })

必须显式请求枚举器的原因是PowerShell默认认为hashtable/dictionary一个单个对象通过管道整体传递/传递给.Where() and .ForEach()数组方法等枚举方法。

请注意,上述命令的输出 不是 本身是一个哈希表,而是一个常规的类似数组的集合([System.Collections.ObjectModel.Collection[psobject] 类型),其元素恰好是键值对对象。
因此,此输出 在管道中枚举。