I have a file containing all config data About a Software we are using. From this file, I Need to extract the names of the Archives and determine how many Archives exist to pass this Information to another script to perform an Export of this Archives using the Archive Names. Archive Names are to be found in this context
{
"name": {
"value": "Archiv1",
"valid": true
},
"displayName": {
"value": "Repository4",
"valid": true
},
"systemLanguage": {
"value": "de",
"valid": true
},
In this Case Archiv1 would be the value I am Looking for. For this Purpose I have created following Script:
findstr /N 'systemLanguage' $dpath\temp\serversetup2\sample.conf > $dpath\temp\serversetup2\temp\findarch.txt
$content = Get-Content $dpath\temp\serversetup2\temp\findarch.txt
$content -split ':' > $dpath\temp\serversetup2\temp\archs.txt
As systemLanguage is a variable for every Archive and doesn´t appear in any other context I am Looking for the Line Number of systemLanguage and saving the results with line number and whole line string into findarch.txt. Afterwards I am simply using a delimeter to seperate the line number from following string, as every line number has following pattern: 123: §Content§. After saving the seperated numbers and strings into archs.txt, I am extracting lines that begin with a number into a new file numlist.txt
foreach($file in (Get-Content -Path $dpath\temp\serversetup2\temp\archs.txt)){
$first_letter=$file.Substring(0,1)
if($first_letter -match '\d'){
Add-Content $dpath\temp\serversetup2\temp\numlist.txt -Value $file
}
}
Using numlist.txt, I (thought, that I) can simply jump back to the line containing the Archive Name using:
$count = 0
foreach($file in (Get-Content -Path $dpath\temp\serversetup2\temp\numlist.txt)){
if($count -eq 0){
$a=$file-10
$count=$count+1}
else{
$a=$file-8
}
Get-Content $dpath\temp\serversetup2\sample.conf | Select-Object -Index $a >> $dpath\temp\serversetup2\temp\archs.txt
}
Finally the line I really Need is being extracted to archs.txt from sample.conf using the new line number.I am using if, to determine first run, as If I have many Archives (more than one), the Content between line containing systemlanguage and the Archive value changes like:
{
"name": {
"value": "Arc9wa",
"valid": true
},
"displayName": {
"value": "Repository5",
"valid": true
},
"systemLanguage": {
"value": "de",
"valid": true
},
but remains as a pattern for other following Archive values. So I thought, I can set a fixed number to Always jump to the correct line to get the Archive value (worked fine on the Computer I worked on).
Last step is to extract the value of the Archive into a new file Archives.txt
foreach($file in (Get-Content -Path $dpath\temp\serversetup2\temp\archs.txt)){
if($file -match ' "value": "(?<content>.*)",'){
$res= $matches['content']
Add-Content $dpath\temp\serversetup2\temp\archives.txt -Value $res
}}
This Code worked fine on the development pc. Afterwards I found out, that every Installation of this Software creates a unique (very similar but different) sample.conf file, so that I cannot use the fixed Operations ($a-10 for example) to jump to the Right line to get the Archive value.
I Need help to Always get to the value I am Looking for. I tried to find unique Keywords to extract data in between and further process to get to the value. The Keywords I found were archiveInfo and hostInfo. I tried to use a script like
$db= Get-Content C:\temp\serversetup2\sample.conf
$db -match ' "archiveList(?<content>.*) "databaseInfo": {'
$res= $matches['content'] > C:\Users\Administrator\Desktop\test.txt
and
$db= Get-Content C:\temp\serversetup2\sample.conf
$db | Select-String -Pattern 'archiveList(?<content>.*)databaseInfo'
$res= $matches['content']
or
Get-Content C:\temp\serversetup2\sample.conf | Select-String -Pattern 'archiveList.*?databaseInfo' |
select -expandproperty matches | select captures > C:\Users\Administrator\Desktop\test.txt
but None of them worked. I appreciate every answer.
ConvertFrom-Jsonto parse the json text and extract the values from the resulting object model? e.g.$data = Get-Content $myFile | ConvertFrom-Json; write-host $data.name.value