Why does my generated JSON have too many "\\"?

Dear Community

I have a Powershell Script which generates a JSON file with data in it.

I have a problem with this file though. It generates double the amount of “\”!

Do you know how I could solve this?

Here is my Code to generate the JSON File:

[ordered]@{
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 | Out-File "$secFile"

 

$secFile is just a path to save the file to. Just tell me if you need this too.

The output JSON file looks liek this though:

{
"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": ""
}

Greetings

Martin

 

Edit: I also posted this question in Stackoverflow and the Microsoft Tech Community, just so you know
https://stackoverflow.com/questions/63446524/why-does-my-generated-json-have-too-many

https://techcommunity.microsoft.com/t5/windows-powershell/why-does-my-generated-json-have-too-many-quot-quot/td-p/1592234

It appears it needs to escape the backslashes. It imports just fine, what’s the actual issue?

[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

$results | 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 : 

When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to help you making their work twice or more.

https://stackoverflow.com/questions/63446524/why-does-my-generated-json-have-too-many

Thanks

Im sorry for the inconvenience. I will learn from my mistake and put the link in

 

Greetings

Martin

Hi

My actual problem is that when it gets put back into place it gets multiplied.

Example before:

share:\\ENTER HERE\C$

 

Example after:

share:\\\\ENTER HERE\\C$

 

Greetings

Martin

I cannot reproduce your issue. When I run the following code …

[ordered]@{
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 | Out-File $secFile

Get-Content -Path $secFile | ConvertFrom-Json

I get this as the result:

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 :