Link to home
Start Free TrialLog in
Avatar of D B
D BFlag for United States of America

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of D B

ASKER

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"?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of D B

ASKER

Thanks.