Improved search-files.ps1

This commit is contained in:
Markus Fleschutz 2021-01-02 13:02:16 +00:00
parent 9f7552b386
commit bad24e40ac

View File

@ -8,7 +8,17 @@
param([string]$Pattern = "", [string]$Path = "") param([string]$Pattern = "", [string]$Path = "")
Set-StrictMode -Version Latest function ListScripts { param([string]$Pattern, [string]$Path)
$List = Select-String -Path $Path -Pattern "$Pattern"
foreach ($Item in $List) {
New-Object PSObject -Property @{
'Path' = "$($Item.Path)"
'Line' = "$($Item.LineNumber)"
'Text' = "$($Item.Line)"
}
}
write-output "(pattern found at $($List.Count) locations)"
}
try { try {
if ($Pattern -eq "" ) { if ($Pattern -eq "" ) {
@ -18,10 +28,7 @@ try {
$Path = read-host "Enter path to files" $Path = read-host "Enter path to files"
} }
$List = Select-String -Path $Path -Pattern "$Pattern" ListScripts $Pattern $Path | format-table -property Path,Line,Text
foreach ($Item in $List) {
write-output "$($Item.Path) @$($Item.LineNumber)`t$($Item.Line)"
}
exit 0 exit 0
} catch { } catch {
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"