2020-12-29 12:03:53 +01:00
|
|
|
#!/snap/bin/powershell
|
2020-12-29 15:14:21 +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 12:03:53 +01:00
|
|
|
|
2020-12-29 15:14:21 +01:00
|
|
|
param([string]$Pattern, [string]$Path)
|
2020-12-29 12:03:53 +01:00
|
|
|
|
|
|
|
Set-StrictMode -Version Latest
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ($Pattern -eq "" ) {
|
|
|
|
$Pattern = read-host "Enter search pattern"
|
|
|
|
}
|
|
|
|
if ($Path -eq "" ) {
|
|
|
|
$Path = read-host "Enter path to files"
|
|
|
|
}
|
|
|
|
|
|
|
|
$List = Select-String -Path $Path -Pattern "$Pattern"
|
|
|
|
foreach ($Item in $List) {
|
|
|
|
write-output "$($Item.Path) @$($Item.LineNumber)`t$($Item.Line)"
|
|
|
|
}
|
|
|
|
exit 0
|
|
|
|
} catch {
|
|
|
|
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
exit 1
|
|
|
|
}
|