Home > Software design >  Use Powershell to find and compare text in file to a folder name
Use Powershell to find and compare text in file to a folder name

Time:09-28

My apologies, but unfortunately my Powershell scripting is quite poor, however I'm trying to muddle on with this... The below is actually for a Nagios check, hence the defining the OK, WARNING etc

Summary

I have 2 small text files with specific text within that I need to check for a folder existing with the corresponding name.

In File 1 below, note the section that states "prod" on line 18, against this I am interested in the apples, pears and bananas data (that which is a date format, plus other text) within the speech marks, so for bananas it would be 20220817-1-64 only.

  • The position within the text file of the "prod" line and subsequent data I'm interested in can't be guaranteed.
  • The data against apples, pears etc will change over time

file1.txt:

{
"default": "prod",
"pre": {
    "apples": "20220711-0",
    "pears": "20220711-0",
    "bananas": "20220711-1-64"
},
"test": {
    "apples": "20220920-0",
    "pears": "20220920-0",
    "bananas": "20220920-1-64"
},
"new": {
    "apples": "20220910-0",
    "pears": "20220910-0",
    "bananas": "20220910-1-64"
},
"prod": {
    "apples": "20220817-0",
    "pears": "20220210-0",
    "bananas": "20220817-1-64"
},
"old": {
    "apples": "20220601-0",
    "pears": "20220601-0",
    "bananas": "20220601-1-64"
}
}

File 2 has a similar principal, I am only interested in 20220923-0 next to the "prod" line (again, position within the file can't be guaranteed and the data will change over time)

File2.txt:

{
"default": "prod",
"pre": "20220917-0",
"test": "20220926-0",
"new": "20220924-0",
"prod": "20220923-0"
}

Each of the values would need to be compared against a directory, to see if a folder of the same name exists. If it matches, the result would be OK, if different then result in a WARNING, if missing the result would be CRITICAL.

What I have tried

Defining the result and folder to check against is straight forward enough:

# Result
$OK=0
$WARNING=1
$CRITICAL=2

# Folders to check
$apples_folder = (Get-Childitem c:\folder_path\apples\*).Name
$pears_folder = (Get-Childitem c:\folder_path\pears\*).Name

However the main part I'm struggling with is picking out the relevant text from the text file(s) against the prod line(s)

From what I have gathered, I suspect using regex or possibly grep commands may hold the answer, but I can't quite get my head around it.

Any pointers in the right direction would be appreciated.

CodePudding user response:

Continuing from my comment, you can parse File1.txt as JSON, assuming that what you gave us as example is missing the final closing bracket '}' as a result of posting it here and that the json in the actual file is complete.

To work with that file type, you can do

$json = Get-Content -Path 'X:\somewhere\File1.txt' -Raw | ConvertFrom-Json
$prodFolder = $json.prod.bananas
# next test if you can find a folder with that name and do whatever you need to do then
if (Test-Path -Path "c:\folder_path\prod\$prodFolder" -PathType Container) {
    "OK"
}
else {
    "CRITICAL"
}

For File2.txt, things are quite different because that is a strange format.. What you can do is convert the data in there into a Hashtable using the ConvertFrom-StringData cmdlet.

In PowerShell 5.x you need to replace the colons that separate the names from their values into an equal sign (=)

$data = (Get-Content -Path 'X:\somewhereElse\File2.txt' -Raw) -replace '^(.*?):(.*)','$1=$2' -replace '[", ]' | ConvertFrom-StringData
$prodFolder = $data.prod
# next test if you can find a folder with that name and do whatever you need to do then
if (Test-Path -Path "c:\folder_path\prod\$prodFolder" -PathType Container) {
    "OK"
}
else {
    "CRITICAL"
}

In PowerShell 7.x you do not have to replace the colons:

$data = (Get-Content -Path 'X:\somewhereElse\File2.txt' -Raw) -replace '[", ]' | ConvertFrom-StringData -Delimiter ':'
  • Related