PowerShell Query to Examine File Contents

Under a root directory, we have multiple subdirectories, each containing, amongst other things, configuration files. Each of these configuration files has a file extension of .config.
Occasionally, in order to manually handle data, a developer will make a change to a file, run the process and then change the file back. But, not always.
Thus, from time-to-time, we are left with a file that runs in an unexpected way in production. The overwhelming number of times a file is changes, one of the properties is overridden. Normally, the line should read:
insert.only=true
But it will be changed to:
insert.only=false

Often, we will have both lines, but one will be preceded with a '#', rendering it a comment to the engine processing the file.
I would like to create a process in PowerShell that will read every file that has an extension of .config, look for a row that starts with 'insert.only', and make sure it ends with 'true'. If one is found with a value other than 'true' (although the only other value should be 'false') then write out the full filename. I don't necessarily need the line number or the actual value, since the files are small, and the only values for the row should be 'insert.only=true', 'insert.only=false', '#insert.only=true' or '#insert.only=false'.

If a line is found that starts with 'insert.only' without the preceding hashtag, and does not end with 'true' then it is in error.

If insert.only is not found, I would also like to optionally report on that. However, I need an easy way to comment out that piece, because I may not be able to filter my input file selection (e.g. which files to search) and if not, those results would be invalid, since only certain types of configuration files use the insert.only parameter.
LVL 15
dbbishopAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
This returns a report for all .config files containing an uncommented "insert.only" setting:
$RootFolder = "D:\Temp"
$IncludeFiles = "*.config"
$Pattern = "\A\s*[^#]?\s*insert\.only\s*=\s*(.+)\Z"
Get-ChildItem -Path $RootFolder -Include $IncludeFiles -Recurse |
	Select-String -Pattern $Pattern |
	Select-Object -Property Path, @{Name="InsertOnly"; Expression={$_.Matches.Groups[1].Value}}

Open in new window

To filter out the "true" values, pipe it to a Where expression:
$RootFolder = "D:\Temp"
$IncludeFiles = "*.config"
$Pattern = "\A\s*[^#]?\s*insert\.only\s*=\s*(.+)\Z"
Get-ChildItem -Path $RootFolder -Include $IncludeFiles -Recurse |
	Select-String -Pattern $Pattern |
	Select-Object -Property Path, @{Name="InsertOnly"; Expression={$_.Matches.Groups[1].Value}} |
	? {$_.InsertOnly -ne "true"}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dbbishopAuthor Commented:
So the second will return all files that have insert.only at the start of the string but that don't have the value set to true?
I want to make sure that I don't get a false report where one line has
insert.only=false
and another line has
#insert.only=true
mainly because there are a number of jobs where there is a pretty frequent need to run manually, so there will be two lines:
insert.only=true
#insert.only=false
and a developer will switch the comment between the two. I want to report where they made the switch and forgot to switch it back.
Also, in the $IncludeFiles = line, I assume I can use more pattern matching, such as
$IncludeFiles = "*fileload*.config"?
0
oBdACommented:
Yes, the regex will ignore any line starting with a "#" as first non-whitespace character.
Note that it will complain as well about a "true" that ends with a whitespace (as in "insert.only=true "); if you want to accept these as well, replace the "(.+)" in line 3 with "(\S+)" (the cAsE of the S mAtTtErS!).
You can set $IncludeFiles to whatever returns the correct files.
0
dbbishopAuthor Commented:
Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.