Issue
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 the PowerShell.org Forum and the Microsoft Tech Community, just so you know
https://powershell.org/forums/topic/why-does-my-generated-json-have-too-many/
Solution
There is a way to escape the JSON:
[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 | Foreach {[System.Text.RegularExpressions.Regex]::Unescape($_)} | Out-File "$secFile"
This will make the backslashes escape. Output:
{
"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": ""
}
Answered By - Wasif Answer Checked By - Gilberto Lyons (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.