mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-11-14 20:14:00 +01:00
Improved search-files.ps1
This commit is contained in:
parent
9f7552b386
commit
bad24e40ac
@ -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])"
|
||||||
|
Loading…
Reference in New Issue
Block a user