Search for Keyword using powershell

Hello -

I have given a task to create a script that checks SQL scripts for strings and will give me the list of sql scripts that have the strings detected.

Currently, my CheckScript is as follow:

Get-ChildItem "D:\IDs" -recurse | select name >> "D:\CheckDBScripts\Schema.txt"

Get-ChildItem "D:\IDs" -recurse | Select-String -pattern "DROP TABLE|SCHEMA" -exclude "DROP TABLE #[A-Z]" | group path | select name >> "D:\CheckDBScripts\Schema.txt"


But this doesn't work.  Eventually, I would like to be able to search for Create, Alter, and excluding any temp table creation, deletion, and alteration.

Please advise.

Thank you!

March 23rd, 2015 4:52pm

 

"DROP TABLE #[A-Z]"

Only searches for # + 1 character.

#.*\S

should do it.

Free Windows Admin Tool Kit Click here and download it now
March 23rd, 2015 5:28pm

The help for the -exclude parameter for select-string notes that it is designed to qualify the path parameter, not the pattern parameter.

This is really a question about how regular expressions work.

Start with the help.

March 23rd, 2015 5:29pm

Like this:

Get-ChildItem "D:\IDs" -recurse | Select-String '(?!DROP TABLE #.*\S)DROP Table|CREATE TABLE'

Free Windows Admin Tool Kit Click here and download it now
March 23rd, 2015 5:39pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics