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?
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.