2017-05-30 36 views
-1
Get-ChildItem -Path "$Loc" -Recurse -Include *.cfg,*.bat,*.xml,*.properties,*.ini,*.config -Exclude *.dll,*.log,*.exe,*log*.txt,*.tmp,*.mdb,*.mdf,*.edb,*.dat,*.zip,*.pem,*.ldf | 
    Select-String -Pattern "password=" | 
    Select-String -Pattern "pass=" | 
    Select-String -Pattern "passwd=" | 
    Select-String -Pattern "passphrase=" | 
    Select-String -Pattern "pass=" | 
    Select-String -Pattern "pwd=" | 
    Select-String -Pattern "psw=" | 
    Select-String -Pattern "<password>" | 
    Select-Object Path, Filename, Pattern, LineNumber, Line 

我想在D驅動器中存在的所有文件中搜索上述模式。處理它需要很多時間。是否有可能通過優化上面的查詢來減少所涉及的時間?查詢需要很多時間來處理單行命令需要幫助來優化它

+1

您應該將通過'|'分隔的模式組合在一個模式字符串中。但是,如果驅動器D有很多文件,那麼Get-ChildItem本身就會很慢,除了直接讀取驅動器的MFT或USN日誌或者可能使用[Everything(http://www.voidtools.com/) )的API。 – wOxxOm

回答

2

包含擴展名隱含排除其他擴展名,
因此完全不需要使用-Exclude參數。

$Extensions = @('*.cfg','*.bat','*.xml','*.properties','*.ini','*.config') 
$ToMatch = 'password=','pass=','passwd=','passphrase=','pwd=','psw=','<‌​password>' 
$Pattern = ($ToMatch | ForEach{[regex]::escape($_)}) -join '|' 
$loc = [environment]::getfolderpath(「userprofile」) 

# -Exclude *.dll,*.log,*.exe,*log*.txt,*.tmp,*.mdb,*.mdf,*.edb,*.dat,*.zip,*.pem,*.ldf 

Get-ChildItem -Path "$Loc\*" -Recurse -Include $Extensions | 
    Select-String -Pattern $Pattern| 
    Select-Object Path, Filename, Pattern, LineNumber, Line 

編輯包括@TheMadTechnician's建議。

+2

這是更多的代碼,但有一件事可以幫助人們確保它們正確地轉義,就是定義一個匹配的字符串數組,然後將每個字符串轉義並用管道將它們連接起來。例如:'$ ToMatch ='password =','pass =','passwd =','passphrase =','pwd =','psw =',''; $ Pattern =($ ToMatch | ForEach {[regex] :: escape($ _)})-join'|'' – TheMadTechnician

+0

謝謝。將嘗試這 – user3232560

+0

@TheMadTechnician尼斯增強,我會將它納入我的腳本。 – LotPings