2021-04-07 11:53:57 +02:00
|
|
|
#!/usr/bin/pwsh
|
2020-12-29 15:14:21 +01:00
|
|
|
<#
|
2021-03-22 20:10:18 +01:00
|
|
|
.SYNTAX ./search-files.ps1 [<pattern>] [<path>]
|
|
|
|
.DESCRIPTION searches the given pattern in the given files
|
|
|
|
.LINK https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES Author: Markus Fleschutz / License: CC0
|
2020-12-29 15:14:21 +01:00
|
|
|
#>
|
2020-12-29 12:03:53 +01:00
|
|
|
|
2021-02-18 20:17:55 +01:00
|
|
|
param($Pattern = "", $Path = "")
|
2020-12-29 12:03:53 +01:00
|
|
|
|
2021-01-02 14:02:16 +01:00
|
|
|
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)"
|
|
|
|
}
|
2020-12-29 12:03:53 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
if ($Pattern -eq "" ) {
|
|
|
|
$Pattern = read-host "Enter search pattern"
|
|
|
|
}
|
|
|
|
if ($Path -eq "" ) {
|
|
|
|
$Path = read-host "Enter path to files"
|
|
|
|
}
|
|
|
|
|
2021-01-02 14:02:16 +01:00
|
|
|
ListScripts $Pattern $Path | format-table -property Path,Line,Text
|
2020-12-29 12:03:53 +01:00
|
|
|
exit 0
|
|
|
|
} catch {
|
2021-02-16 10:03:20 +01:00
|
|
|
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
2020-12-29 12:03:53 +01:00
|
|
|
exit 1
|
|
|
|
}
|