Reputation: 393
I will have a reoccurring search for strings in a defined file in a larger script.
function ConfigSearch([String] $path, [String] $pattern){
[string]$path
[string]$pattern
Get-ChildItem -path $path | Select-String -pattern $pattern
}
ConfigSearch c:\smhost SMHOST
This is an example. Output will be
PS C:\Backup> C:\test\filesearch.ps1
c:\smhost
SMHOST
C:\smhost\smhost.conf:1:SMHOST = "D:\ca\webagent cr010\win64\config\smhost.conf"
I don't want anything else but C:\smhost\smhost.conf:1:SMHOST = "D:\ca\webagent cr010\win64\config\smhost.conf"
. Or even more prefered "D:\ca\webagent cr010\win64\config\smhost.conf"
, the content that I'm looking for.
Do you mean add them like this:
function ConfigSearch([String] $path, [String] $pattern){
[string]$path
[string]$pattern
$Matches = Get-ChildItem -path $path | Select-String -pattern $pattern
$Matches | Select-Object -ExpandProperty Line
}
ConfigSearch c:\smhost SMHOST
One problem solved, but the output now is
c:\smhost
SMHOST
SMHOST = "D:\ca\webagent cr010\win64\config\smhost.conf"
And I don't want
c:\smhost
SMHOST
to be outputted.
Upvotes: 1
Views: 141
Reputation: 174760
Select-String
returns a selection info object, you can select just the matched line if you want to:
$Match = Get-ChildItem -path $path | Select-String -pattern $pattern
$Match | Select-Object -ExpandProperty Line
Upvotes: 3