Ton*_*ony 2 powershell hashset
我的脚本有问题:
这里我设置了一个 var Remotefilehash:
$remotefilehash = $remotefiles -replace '^[a-f0-9]{32}( )', '$0= ' | ConvertFrom-StringData
Run Code Online (Sandbox Code Playgroud)
这创建了一个哈希表
然后我将这些哈希值与本地哈希集中的值进行比较
# 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($_) })
Run Code Online (Sandbox Code Playgroud)
这让我得到了哈希键,但我随后还需要获取在上面找到的那些键的哈希值......
这创建了一个哈希表
实际上,它创建了一个哈希表数组,因为将每个管道输入对象转换为一个单独的哈希表。ConvertFrom-StringData
要创建单个哈希表,请首先连接输入行以形成单个字符串 - 请注意如何-join "`n"将 应用于操作结果-replace以形成单个多行字符串:
$remotefilehash =
($remotefiles -replace '^[a-f0-9]{32}( )', '$0= ' -join "`n") |
ConvertFrom-StringData
Run Code Online (Sandbox Code Playgroud)
要枚举键值对而不仅仅是哈希表(类似)对象的键(.Keys),您需要使用它的.GetEnumerator()方法:
$diffmd5 =
$remotefilehash.GetEnumerator().Where({ -not $localHashSet.Contains($_.Key) })
Run Code Online (Sandbox Code Playgroud)
必须显式请求枚举器的原因是,PowerShell 默认情况下将哈希表/字典视为单个对象,该对象作为整体通过管道传递/传递给枚举方法(例如.Where()和.ForEach()数组方法)。
请注意,上述命令的输出本身并不是一个哈希表,而是一个常规的、类似数组的集合(类型为[System.Collections.ObjectModel.Collection[psobject]),其元素恰好是键值对对象。
因此,该输出在管道中被枚举。
| 归档时间: |
|
| 查看次数: |
641 次 |
| 最近记录: |