ConvertFrom-StringData Duplicates



如何最好地消除此行的任何数据重复?唯一的问题似乎是ConvertFromStringData;如果我去掉那一位,我就不会出错。。。

这是代码:

$remotefilehash =  (($remoteFiles | Where-Object { -not ($_ | Select-String -Quiet -NotMatch -Pattern '^[a-f0-9]{32}(  )') }) -replace '^[a-f0-9]{32}(  )', '$0=  ' -join "`n") | ConvertFrom-StringData

和错误

ConvertFrom-StringData : Data item 'a3512c98c9e159c021ebbb76b238707e' in line 'a3512c98c9e159c021ebbb76b238707e  =  My Pictures/Tony/Automatic Upload/Tony’s iPhone/2022-10-08 21-46-21.mov' is already 
defined.

$remotefiles变量的数据如下:

a3512c98c9e159c021ebbb76b238707e  =  My Pictures/Tony/Automatic Upload/Tony’s iPhone/2022-10-08 21-46-21 (2).mov
a3512c98c9e159c021ebbb76b238707e  =  My Pictures/Tony/Automatic Upload/Tony’s iPhone/2022-10-08 21-46-21.mov

所以我只需要其中一个文件,因为它们都有相同的校验和,我不在乎的路径

我在想也许可以试试";已经定义了"?也许这更好b/c如果真的发生了,我可以运行不同的命令

我个人会这样做,在每个字符串上只循环一个ForEach-Object-match$Matches用于正则表达式比较和生成对象,HashSet<T>确保代码不会输出重复的哈希:

$remoteFiles = @'
a3512c98c9e159c021ebbb76b238707e  =  path/to/thing/2022-10-08 21-46-21 (2).mov
a3512c98c9e159c021ebbb76b238707e  =  path/to/thing/2022-10-08 21-46-21.mov
a3512c98c9e159c021ebbb76b238707f  =  path/to/otherstuff
a3512c98c9e159c021ebbb76b238707f  =  path/to/otherstuff2
a3512c98c9e159c021ebbb76b238707f  =  path/to/otherstuff3
'@ -split 'r?n'
$hash = [Collections.Generic.HashSet[string]]::new([StringComparer]::OrdinalIgnoreCase)
$remoteFiles | ForEach-Object {
if($_ -match '(?<hash>^[a-f0-9]{32})[s=]{5}(?<path>.+)' -and $hash.Add($Matches['hash'])) {
[pscustomobject]@{
Hash = $Matches['hash']
Path = $Matches['path']
}
}
}

输出:

Hash                             Path
----                             ----
a3512c98c9e159c021ebbb76b238707e path/to/thing/2022-10-08 21-46-21 (2).mov
a3512c98c9e159c021ebbb76b238707f path/to/otherstuff

最新更新