Whit the help of the contributors @mklement0 and @Theo, I could get good results with the solution explained in this question Powershell ConvertTo-Json problem with double-quotation marks.
But due to the requirements of the JSON-format, I have two more issues:
- The values with the mac-and ip-addresses should be indicated between square brackets ([ ]) and each one should be indicated between (" "), e.g.:
"mac_address": ["00:10:XX:10:00:0X", "X0:X0:11:X0:00:0X", "X0:11:X0:11:XX:11"]
- In the file.txt, I will also have the following key-value information:
product_executable : "C:\Program Files (x86)\McAfee\VirusScan Enterprise\SHSTAT.EXE" /!REMEDIATE
I used the current code in the question mentioned above and I get double backslash and a new one at the beginning:
"product_executable": "\"C:\\Program Files (x86)\\McAfee\\VirusScan Enterprise\\SHSTAT.EXE\" /!REMEDIATE"
I already tried to handle the hashtable output ($oht) without success as well as trying to modify add and delete characters.
My file.txt contains the following structured information (two empty lines at the beginning as well):
adapter_name : empty1
route_age : 10
route_nexthop : 172.0.0.1
route_protocol : NETMGMT1
speed : null
mac_address : 11:10:XX:10:00:0X, X1:X0:11:X0:00:0X, X1:11:X0:11:XX:11
product_executable : "C:\Program Files (x86)\McAfee\VirusScan Enterprise\SHSTAT.EXE" /!REMEDIATE
adapter_name : empty2
route_age : 100
route_nexthop : 172.0.0.2
route_protocol : NETMGMT2
speed : null
mac_address : 22:10:XX:10:00:0X, X2:X0:11:X0:00:0X, X2:11:X0:11:XX:11
product_executable : "C:\Program Files (x86)\McAfee\VirusScan Enterprise\SHSTAT.EXE" /!REMEDIATE
adapter_name : empty3
route_age : 1000
route_nexthop : 172.0.0.3
route_protocol : NETMGMT3
speed : null
mac_address : 33:10:XX:10:00:0X, X3:X0:11:X0:00:0X, X3:11:X0:11:XX:11
product_executable : "C:\Program Files (x86)\McAfee\VirusScan Enterprise\SHSTAT.EXE" /!REMEDIATE
The current code (see question mentioned) is:
# Read the input file as a whole (-Raw) and split it into blocks (paragraphs)
(Get-Content -Raw C:\scripts\file.txt) -split '\r?\n\r?\n' -ne '' |
ForEach-Object { # Process each block
# Initialize an ordered hashtable for the key-values pairs in this block.
$oht = [ordered] @{}
# Loop over the block's lines.
foreach ($line in $_ -split '\r?\n' -ne '') {
# Split the line into key and value...
$key, $val = $line -split ':', 2
# ... and add them to the hashtable.
$oht[$key.Trim()] = $val.Trim()
}
$oht # Output the hashtable.
} | ConvertTo-Json
The actual result is:
[
{
"adapter_name" : "empty1",
"route_age" : 10,
"route_nexthop" : "172.0.0.1",
"route_protocol" : "NETMGMT1",
"speed" : null,
"mac_address" : "11:10:XX:10:00:0X, X1:X0:11:X0:00:0X, X1:11:X0:11:XX:11",
"product_executable" : "\"C:\\Program Files (x86)\\McAfee\\VirusScan Enterprise\\SHSTAT.EXE" /!REMEDIATE"
},
{
"adapter_name" : "empty2",
"route_age" : 100,
"route_nexthop" : "172.0.0.2",
"route_protocol" : "NETMGMT2",
"speed" : null,
"mac_address" : "22:10:XX:10:00:0X, X2:X0:11:X0:00:0X, X2:11:X0:11:XX:11",
"product_executable" : "\"C:\\Program Files (x86)\\McAfee\\VirusScan Enterprise\\SHSTAT.EXE" /!REMEDIATE"
},
{
"adapter_name" : "empty3",
"route_age" : 1000,
"route_nexthop" : "172.0.0.3",
"route_protocol" : "NETMGMT3",
"speed" : null,
"mac_address" : "33:10:XX:10:00:0X, X3:X0:11:X0:00:0X, X3:11:X0:11:XX:11",
"product_executable" : "\"C:\\Program Files (x86)\\McAfee\\VirusScan Enterprise\\SHSTAT.EXE" /!REMEDIATE"
}
]
And the expected result is:
[
{
"adapter_name" : "empty1",
"route_age" : 10,
"route_nexthop" : "172.0.0.1",
"route_protocol" : "NETMGMT1",
"speed" : null,
"mac_address" : ["11:10:XX:10:00:0X", "X1:X0:11:X0:00:0X", "X1:11:X0:11:XX:11"],
"product_executable" : ""C:\Program Files (x86)\McAfee\VirusScan Enterprise\SHSTAT.EXE" /!REMEDIATE"
},
{
"adapter_name" : "empty2",
"route_age" : 100,
"route_nexthop" : "172.0.0.2",
"route_protocol" : "NETMGMT2",
"speed" : null,
"mac_address" : ["22:10:XX:10:00:0X", "X2:X0:11:X0:00:0X", "X2:11:X0:11:XX:11"],
"product_executable" : ""C:\Program Files (x86)\McAfee\VirusScan Enterprise\SHSTAT.EXE" /!REMEDIATE"
},
{
"adapter_name" : "empty3",
"route_age" : 1000,
"route_nexthop" : "172.0.0.3",
"route_protocol" : "NETMGMT3",
"speed" : null,
"mac_address" : ["33:10:XX:10:00:0X", "X3:X0:11:X0:00:0X", "X3:11:X0:11:XX:11"],
"product_executable" : ""C:\Program Files (x86)\McAfee\VirusScan Enterprise\SHSTAT.EXE" /!REMEDIATE"
}
]
I would really appreciate your suggestions.