0

我将一个大 Json 读入 powershell,然后通过减少字段

select-object -Property $myProperties

它运行良好,但速度很慢。

那里有更多的性能替代品吗?

我读了一些关于 linq 的内容,但我不确定这是否能解决我的问题。我还考虑过通过 convertfrom-json 中的 -ashashtable 运算符将内容读取为哈希表,然后通过 .remove 运算符删除不必要的属性,但它需要对数组进行额外处理。

有没有人有一般建议,首选的解决方案是什么?

完整脚本

function Start-TestDatabaseFile {
    param (
        $folderSoll,
        $folderIst
    )

    $workfolderPath = $workfolder.path

    $errors = 0

    $dbInfo = Get-dbInformation

    $localFolder = "$workfolderPath\TestResults\DatabaseFileDifference"
    New-Item $localFolder -ItemType Directory | Out-Null

    $differencePerTable = "$workfolderPath\TestResults\DatabaseFilePerTable"
    New-Item $differencePerTable -ItemType Directory | Out-Null

    $keys = $result.Keys | Sort-Object

    foreach ($key in $keys) {
        #region File Datenmodell
        $firstItemFile = $result[$key].fileRaw.Replace('_response.json', '_Datenbank.json')

        $tf = $result[$key].tf
        $myTestfall = $myTest[$tf].root

        if ($null -ne $myTestfall) {
            $responseFile = Split-Path $firstItemFile -Leaf
            $myPath = $result[$key].myPath
            $file = Join-Path $myTest[$myTestfall].folder -ChildPath ("$myPath\$responseFile")
            $secondItemFile = $file.Replace('\Requests\', '\Responses_SOLL\')
        }
        else {
            $secondItemFile = $firstItemFile.replace($folderIst, $folderSoll)
        }

        try {
            $firstItemAll = Get-Content $firstItemFile -ErrorAction Stop | ConvertFrom-Json -AsHashtable
            $firstItem = $firstItemAll.post
            $firstItemExists = $true
        }
        catch {
            $firstItem = '{}' | ConvertFrom-Json -AsHashtable
            $firstItemExists = $false
        }

        try {
            $secondItemAll = Get-content $secondItemFile -ErrorAction Stop | ConvertFrom-Json -AsHashtable
            $secondItem = $secondItemAll.post
            $secondItemExists = $true
        }
        catch {
            $secondItem = '{}' | ConvertFrom-Json -AsHashtable
            $secondItemExists = $false
        }

        $views = $dbInfo.keys | Where-Object { $_ -like 'V*' }

        foreach ($view in $views) {
            $schalter = $dbInfo[$view].myItem
            if ($firstItemExists) {
                try {
                    $firstItem.$view = [array]($firstItem.$view | Select-Object -Property $dbInfo[$view].fieldsCompare)
                }
                catch {
                    [console]::WriteLine('Issue in Start-TestDatabaseFile ExceptionPoint1')
                }
            }

            if ($secondItemExists) {
                if ($null -ne $myTestfall) {
                    $secondItem.$view = $secondItemAll.pre.$view
                }

                if ($useNewDm.$schalter) {
                    $secondItem.$view = [array]($secondItem.$view | Select-Object -Property $dbInfo[$view].fieldsCompare)
                }
                else {
                    $secondItem.$view = [array]($firstItemAll.pre.$view | Select-Object -Property $dbInfo[$view].fieldsCompare)
                }
            }
        }

        $firstItemJson = $firstItem | ConvertTo-Json -Depth 100
        $secondItemJson = $secondItem | ConvertTo-Json -Depth 100

        $result[$key].resultDatabaseFile = Compare-Json -ist $firstItemJson -soll $secondItemJson
        $result[$key].passedDatabaseFile = 'passed'

        if ($null -ne $result[$key].resultDatabaseFile.difference) {
            $filename = $key.replace('\', '_')

            $result[$key].resultDatabaseFile.difference | Out-File -FilePath ("$localFolder\$filename.json")

            #region tmp out
            $difference = $result[$key].resultDatabaseFile.difference | ConvertFrom-Json
            $diffKeys = (Get-Member -InputObject $difference -MemberType NoteProperty).Name

            foreach ($diffKey in $diffKeys) {

                $file = "$differencePerTable\$diffKey.json"

                Add-Content -Path $file -Value $key
                Add-Content -Path $file -Value ($difference.$diffkey | ConvertTo-Json)
            }
            #endregion

            $result[$key].passedDatabaseFile = 'failed'

            $errors += 1
        }
        #endregion
    }

    return $errors
}
4

0 回答 0