我的腳本有問題:
在這里我設定了一個 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($_) })
這為我提供了哈希鍵,但隨后我還需要獲取在上面找到的那些鍵的哈希值,其中...
uj5u.com熱心網友回復:
這將創建一個哈希表
實際上,它創建了一個陣列的哈希表,因為ConvertFrom-StringData接通每個流水線輸入物件到一個單獨的哈希表。
要創建單個哈希表,請先連接輸入行以形成單個字串:
$remotefilehash =
($remotefiles -replace '^[a-f0-9]{32}( )', '$0= ' -join "`n") |
ConvertFrom-StringData
要列舉鍵值對而不僅僅是哈希表(類似)物件的鍵( .Keys),您需要使用其.GetEnumerator()方法:
$diffmd5 =
$remotefilehash.GetEnumerator().Where({ -not $localHashSet.Contains($_.Key) })
必須顯式請求列舉器的原因是,PowerShell 默認將哈希表/字典視為單個物件,該物件作為一個整體通過管道傳遞/傳遞給列舉方法,例如 the.Where()和.ForEach()array 方法。
請注意,上述命令的輸出本身不是一個哈希表,而是一個常規的、類似陣列的集合(型別[System.Collections.ObjectModel.Collection[psobject]),其元素恰好是鍵值對物件。
因此,該輸出是在管道中列舉。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/345222.html
