忽略从JSON加载的对象获取ChildItem中的文件

忽略从JSON加载的对象获取ChildItem中的文件,json,powershell,Json,Powershell,我有一个脚本,用于读取JSON文件、读取路径、计算MD5散列,然后将它们导出到CSV文件中。JSON文件包含多个对象。在另一篇文章中,我列出了JSON文件的结构,我将在这里发布。请注意,我不允许更改此JSON { "destinationpath": "C:\\Destination\\Mobile Phones\\" "sourcepath": "C:\\Source\\Mobile Phones\\" "

我有一个脚本,用于读取JSON文件、读取路径、计算MD5散列,然后将它们导出到CSV文件中。JSON文件包含多个对象。在另一篇文章中,我列出了JSON文件的结构,我将在这里发布。请注意,我不允许更改此JSON

{
"destinationpath": "C:\\Destination\\Mobile Phones\\"
"sourcepath": "C:\\Source\\Mobile Phones\\"
"OnePlus" : {
"files": [
{
"source": "6T",
"destination": "Model\\6T",
"log": "logfiles",
"version": "Version.txt",
}
]
}
"Samsung" : {
"files": [
{
"source": "S20",
"destination": "Galaxy\\S20",
"log": "logfiles",
"version": "Version.txt",
}
]
}
}
destinationpath:C:\\Destination\\Mobile Phones\\
,当添加到例如三星
Samsung
时,它将变成:
destinationpath:C:\\Destination\\Mobile Phones\\Galaxy\\S20

到目前为止,我的剧本是:

$JSON = Get-Content -Path file.json | ConvertFrom-Json
$JSONdestination = $JSON.destination
$JSONsource = $JSON.source


foreach ($i in $JSON.psobject.properties) {
    foreach($i2 in $i.Value ){
        $IgnoredFiles = @(
            $logfiles = $i2.files.log
            $newFiles = $i2.files.version
        )
        $destination = $JSON + $i2.files.destination
        $source = $JSONdestination + $i2.files.source
        echo $destination
        Get-ChildItem -Path $destination -Recurse -Exclude $IgnoredFiles | Get-FileHash -Algorithm MD5 -ErrorAction SilentlyContinue | Export-csv -Append -Path "C:\Users\home\Downloads\hashes.csv"
}
Get ChildItem
部分中,我想排除
“log”
“version”
文件,以便它们不计入
Get FileHash
中。我也尝试过这样做:

Get-ChildItem -Path $destination -Recurse -Exclude $i2.files.log, $i2.files.version | Get-FileHash -Algorithm MD5 -ErrorAction SilentlyContinue | Export-csv -Append -Path "C:\Users\home\Downloads\hashes.csv"

但是,它也不起作用,导出的csv中仍会考虑
日志
版本
文件。

您显示给我们的json无效(缺少逗号和逗号太多..)

一旦固定到

{
    "destinationpath": "C:\\Destination\\Mobile Phones\\",
    "sourcepath": "C:\\Source\\Mobile Phones\\",
    "OnePlus": {
        "files": [{
            "source": "6T",
            "destination": "Model\\6T",
            "log": "*.log",
            "version": "Version.txt"
        }]
    },
    "Samsung": {
        "files": [{
            "source": "S20",
            "destination": "Galaxy\\S20",
            "log": "*.log",
            "version": "Version.txt"
        }]
    }
}
您可以这样做:

$JSON = Get-Content -Path file.json | ConvertFrom-Json

$JSONdestination = $JSON.destinationpath
$JSONsource      = $JSON.sourcepath

$result = foreach ($item in ($JSON | Select-Object * -ExcludeProperty destinationpath, sourcepath)) {
    $item.PSObject.Properties.Value | ForEach-Object {
        $destPath  = Join-Path -Path $JSONdestination -ChildPath $_.files.destination
        $logFolder = Join-Path -Path $JSONdestination -ChildPath $_.files.log
        $exclude = $_.files.version
        Write-Host "Processing files in '$destPath'" -ForegroundColor Cyan
        Get-ChildItem -Path $destPath -Recurse -Exclude $exclude -ErrorAction SilentlyContinue | 
            Where-Object {$_.FullName -notlike "$logFolder*" } |
            Get-FileHash -Algorithm MD5
    }
}

$result | Export-Csv -Path "C:\Users\home\Downloads\hashes.csv" -NoTypeInformation

这不可能是正确的:
$destination=$JSON+$i2.files.destination
因为
$JSON
不是路径的一部分,所以它是您在读取
file.JSON
时创建的对象,并从JSON内容转换而来。@Theo是的,这是一个输入错误。实际上是
$JSONdestination
我收到了以下错误:
Get ChildItem:无法将'System.Object[]转换为参数'Exclude'所需的'System.String'类型。不支持指定的方法。
@Aithorusa例如,在您向我们展示的内容中,
“日志”:“日志文件”
实际上不适合用作文件名模式。。这就是为什么我将其更改为
“log”:“*.log”
我正在使用PowerShell 5.1。很抱歉,我不太明白你所说的现实生活模式是什么意思,但这个JSON文件就是我们正在使用的。我为我的拼写错误和缺少逗号而道歉。我只是做了一个复制品,好让大家了解我的想法。日志和版本文件的工作方式与目标路径和源路径相同(如从
destinationpath
获取完整路径)。每个
Samsung
OnePlus
都有
log
version
。在为散列创建CSV时,我需要忽略这些内容,只考虑目标文件。@Aithorusa但完全不清楚“日志”包含的内容<代码>“version”包含有效的文件模式
version.txt
,但
的“log”
类似于
“logfiles”
。这是实际的文件名,或者是我们应该忽略的子文件夹。日志文件有什么扩展名?
logfiles
本身是一个包含
.txt
文件的文件夹,并且有子文件夹。