gpt4 book ai didi

powershell - 键和值的哈希集

转载 作者:行者123 更新时间:2023-12-02 01:54:33 26 4
gpt4 key购买 nike

我的脚本有问题:

这里我设置了一个var remotefilehash:

$remotefilehash = $remotefiles -replace '^[a-f0-9]{32}(  )', '$0=  ' | 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

实际上,它创建了一个 hashtables数组 ,因为ConvertFrom-StringData每个管道输入对象转换为一个单独的哈希表。

要创建单个哈希表,请首先连接输入行以形成单个字符串 - 请注意-join“`n”的方式应用于 -replace 操作的结果以形成单个多行字符串:

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

要枚举哈希表(类似)对象的键值对,而不仅仅是枚举 (.Keys),您可以需要使用其 .GetEnumerator() 方法:

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

必须明确请求枚举器的原因是,PowerShell 默认情况下将哈希表/字典视为一个单个对象,作为一个整体传递 em> 通过管道/传递给枚举方法如.Where().ForEach()数组方法。

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

关于powershell - 键和值的哈希集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69782447/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com