Search code examples
jsonwindowspowershellcertificateweb-deployment

How can I convert a parsed json file from json then back to json?


I am generating a parsed JSON file using powershell ConvertTo-Json. Everything is fine until here, because now I want to convert it from json, use the values and then convert it back to json with the values like they were before. But when I convert the file back to json, it only shows null for the values... Is there any way to solve this?

Here is my code for creating the parsed file:

$secFile = "C:\some\folder\creds.json"
$in = Get-Content $secFile | ConvertFrom-Json
[ordered]@{
        pcname='ENTER HERE';
        share='\\$in.pcname\C$';
        filename='ENTER HERE';
        destfilepath='Scripts\Cert';
        destfile='$in.share\$in.destfilepath\$in.filename';
        RDdestfile='C:\$in.destfilepath\';
        Username="ENTER HERE";
        Password="ENTER HERE";
        EncryptedPassword=""
    } | ConvertTo-Json | Foreach {[System.Text.RegularExpressions.Regex]::Unescape($_)} | Out-File "$secFile"

Here is the code for converting the file back to json:

[ordered]@{
            pcname=$in.pcname;
            share=$in.share;
            filename=$in.filename;
            destfilepath=$in.destfilepath;
            destfile=$in.destfile;
            RDdestfile=$in.RDdestfile;
            Username=$in.Username;
            Password="";
            EncryptedPassword="$secString"
        } | ConvertTo-Json | Out-File "$secFile"

and here is the file after converting it back to json:

{
    "pcname":  null,
    "share":  null,
    "filename":  null,
    "destfilepath":  null,
    "destfile":  null,
    "RDdestfile":  null,
    "Username":  null,
    "Password":  "",
    "EncryptedPassword":  "01000000d08c9ddf0115d1118c7a00c04fc297eb010000006f6d3ce161a681428efe68b51827a6640000000002000000000003660000c0000000100000002a6a1ff60cb280662a9578cb47926a4d0000000004800000a000000010000000a65a1cd7137935dbfcd22bcdc685f52a20000000b87554b4f6f6dbe655cd525a894e1c7d1180b4db121385e57b218fa772ad1d441400000048453bb6e137ed437de3e4ecbd855429ddfc1fba"
}

This worked before I parsed the file. So that can't be the error, right?

I'm neither a powershell or json pro, so I really am hoping for good help

Greetings

Martin


Solution

  • If I take your example and export and reimport, I get an error.

    [pscustomobject]@{
    
        pcname='ENTER HERE';
        share='\\ENTER HERE\C$';
        filename='ENTER HERE';
        destfilepath='some\folder';
        #destfile='$in.share\$in.destfilepath\$in.filename';
        RDdestfile='C:\$in.destfilepath\';
        Username="ENTER HERE";
        Password="ENTER HERE";
        EncryptedPassword=""
    
    } | Convertto-Json -OutVariable results |
        Foreach {[System.Text.RegularExpressions.Regex]::Unescape($_)} |
            Out-File $secFile
    
    Get-Content $secFile | Convertfrom-json
    

    Error

    At line:14 char:28
    +     Get-Content $secFile | Convertfrom-json
    +                            ~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
        + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
    

    However if I simply remove the regex unescaping, it works fine.

    [pscustomobject]@{
    
        pcname='ENTER HERE';
        share='\\ENTER HERE\C$';
        filename='ENTER HERE';
        destfilepath='some\folder';
        #destfile='$in.share\$in.destfilepath\$in.filename';
        RDdestfile='C:\$in.destfilepath\';
        Username="ENTER HERE";
        Password="ENTER HERE";
        EncryptedPassword=""
    
    } | Convertto-Json -OutVariable results| Out-File $secFile
    
    Get-Content $secFile | Convertfrom-json
    

    Output

    pcname            : ENTER HERE
    share             : \\ENTER HERE\C$
    filename          : ENTER HERE
    destfilepath      : some\folder
    RDdestfile        : C:\$in.destfilepath\
    Username          : ENTER HERE
    Password          : ENTER HERE
    EncryptedPassword : 
    

    Is that required?