f-bader / SentinelARConverter

Sentinel Analytics Rule converter PowerShell module
MIT License
53 stars 11 forks source link

Could not convert source file. JSON might be corrupted #18

Closed Konverto-MartinGasser closed 1 year ago

Konverto-MartinGasser commented 1 year ago

Prerequisites

Steps to reproduce

Using PowerShell < v6.2 the conversation is failing with error "Could not convert source file. JSON might be corrupted", since the -Depth parameter for ConvertFrom-Json is only available since PS v6.2. The conversation is working correctly when removing the parameter.

Trusting the answer of mklement0 the parameter could be removed since the default depth level is set to 1024.

Expected behavior

Converting the rule without having PS 6.2 or setting a minimum version requirement.

Actual behavior

Get-Content .\Scheduled.json | Convert-SentinelARArmToYaml -Verbose
Could not convert source file. JSON might be corrupted
In C:\Program Files\WindowsPowerShell\Modules\SentinelARConverter\2.0.1\public\Convert-SentinelARArmToYaml.ps1:203
Zeichen:13
+             throw "Could not convert source file. JSON might be corru ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (Could not conve...ht be corrupted:String) [], RuntimeException
    + FullyQualifiedErrorId : Could not convert source file. JSON might be corrupted

Error details

No response

Environment data

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.22621.1778
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.22621.1778
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Visuals

No response

Manbearpiet commented 1 year ago

Hmm I run into the same error when I test on Windows PowerShell (we should make a job for this).

            if ($PsCmdlet.ParameterSetName -eq "Pipeline") {
                if ($PSVersionTable.PSEdition -ne "Core") {
                    $AnalyticsRuleTemplate = $FullARM | ConvertFrom-Json -Verbose
                } else {
                    $AnalyticsRuleTemplate = $FullARM | ConvertFrom-Json -Depth 99
                }
            } else {
                Write-Verbose "Read file `"$Filename`""

                if ($PSVersionTable.PSEdition -ne "Core") {
                    $AnalyticsRuleTemplate = Get-Content $Filename | ConvertFrom-Json -Verbose
                } else {
                    $AnalyticsRuleTemplate = Get-Content $Filename | ConvertFrom-Json -Depth 99 -Verbose
                }
            }

Should fix it :), when I test with this it works for me.

Manbearpiet commented 1 year ago

Opened #19

f-bader commented 1 year ago

Fixed version is now available on the PowerShell Gallery https://www.powershellgallery.com/packages/SentinelARConverter/2.0.2