locked
Search for Keyword using powershell RRS feed

  • Question

  • 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!

    • Moved by Bill_Stewart Monday, April 27, 2015 8:43 PM Poor quality question/shows no research effort
    Monday, March 23, 2015 8:49 PM

Answers

  • 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.


    PS C:\> help about_Regular_Expressions


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Bill_Stewart Saturday, April 18, 2015 8:00 PM
    • Marked as answer by Bill_Stewart Monday, April 27, 2015 8:42 PM
    Monday, March 23, 2015 9:26 PM

All replies

  •  

    "DROP TABLE #[A-Z]"

    Only searches for # + 1 character.

    #.*\S

    should do it.


    ¯\_(ツ)_/¯

    Monday, March 23, 2015 9:25 PM
  • 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.


    PS C:\> help about_Regular_Expressions


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Bill_Stewart Saturday, April 18, 2015 8:00 PM
    • Marked as answer by Bill_Stewart Monday, April 27, 2015 8:42 PM
    Monday, March 23, 2015 9:26 PM
  • Like this:

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


    ¯\_(ツ)_/¯

    Monday, March 23, 2015 9:36 PM