0

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.

1
  • 2
    Have you looked at ConvertFrom-Json to 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 Commented Aug 12, 2020 at 9:32

1 Answer 1

2

Instead of trying to parse the json yourself, you can use the built-in cmdlets to convert the json text into a structured object model that you can walk to access the properties you're interested in.

This will help avoid issues with things line breaks and other whitespace messing up the line numbers, or escaped characters in the data not being handled properly.

Something like this should work:

$data = Get-Content $myFile | ConvertFrom-Json;

write-host $data.name.value
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.