我正在使用这里引用的代码:与示例等一起使用,但我遇到了一个错误:
$remotefilehash = ($remotefiles -replace '^[a-f0-9]{32}( )', '$0= ' -join "`n") | ConvertFrom-StringData
https://stackoverflow.com/a/69782527/800592
这是完整的错误:
ConvertFrom-StringData : Data item '4826367e20469078693dced6dc642b96' in line '4826367e20469078693dced6dc642b96 = My Videos/Drone/DJI-FPV/DJI_0003.MP4' is already defined.
At P:scriptscodepcloud_sync.ps1:66 char:86
+ ... ace '^[a-f0-9]{32}( )', '$0= ' -join "`n") | ConvertFrom-StringData
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [ConvertFrom-StringData], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.ConvertFromStringDataCommand
Update2我想我的愿望是消除我的remoteFiles
对象的重复数据,它只是一个列表/arry?的md5sum文件路径。由于有时在两个不同的路径中有相同的文件,我想在md5sum 上消除remoteFiles obj的重复
ConvertFrom-StringData
将一行或多行<name>=<value>
形式的每个输入字符串解析为哈希表。
这样做时,不允许重复;也就是说,如果在给定的输入字符串中再次遇到前一个<name>
值,则会出现您看到的语句终止错误。
一个简单的再现:
# !! Error:
# !! "ConvertFrom-StringData: Data item 'Key' in line 'Key=b' is already defined"
ConvertFrom-StringData "Key=a`nKey=b"
因此,解决方案是首先从输入字符串中消除重复项——假设具有重复<name>
s的行中的<value>
部分也相同。
如果没有,您将需要一个不同的数据结构来表示您的输入数据,而ConvertFrom-StringData
无法提供这种数据结构。
如果您只关心<name>
部分——忽略重复部分,而不考虑它们的<value>
——您可以将它们解析为[System.Collections.Generic.HashSet[T]
实例;在您的情况下:
$remotefileHashSet =
[System.Collections.Generic.HashSet[string]] $remotefiles.ForEach({ (-split $_)[0] })
请注意,对于查找,哈希集默认情况下区分大小写;如果您想要区分大小写,还需要做更多的工作:请参阅以下答案。