Hi,
I wrote a script which will find a string in a folder/subfolder and return output to file.
$servers = Import-Csv C:\Users\venkatak\Desktop\Test\all.csv
$Text = “Test”
$Results = “C:\Users\venkatak\Desktop\Test\logtest.txt”
FOREACH ($server in $servers) {
$Path = ‘\’ + $server.ServerName + '\c$\opscripts'
Write-Host $server
If (Test-Path $Path){
get-ChildItem $Path -Recurse |
ForEach-Object {
if ( Get-Content $_.FullName | Select-String -Pattern $sitescope ) {
# Item was found
Write-Output $_ | Select Line,LineNumber,Filename
$Results += @{
'Server' = $Server.ServerName
'SearchTerm' = $SearchTerm
'FilePath' = $_.FullName
'Line' = $_.Line
'LineNumber' = $_.LineNumber
}
}
}
}
}
But, I am not able to get the output log. Is something I am missing?
-Kalyan
You have this:
$Results = "C:\Users\venkatak\Desktop\Test\logtest.txt"
Then this:
$Results += @{ ...
What you’re trying to do is this:
#set a log path
$logPath = 'C:\Users\venkatak\Desktop\Test\logtest.txt'
#create an empty array
$results = @()
#add results to the array
$results += @{
'Server' = $Server.ServerName
'SearchTerm' = $SearchTerm
'FilePath' = $_.FullName
'Line' = $_.Line
'LineNumber' = $_.LineNumber
}
#write the results array to the log file
$results | Out-File $logPath -append
Hi,
Thanks. I updated the script, but still some issues, not able to get the line number in the output & the path is not showing completely.
Completed script:
$servers = Import-Csv C:\Users\venkatak\Desktop\Test\all.csv
$SearchTerm = “good”
#set a log path
$logPath = ‘C:\Users\venkatak\Desktop\Test\logtest.txt’
#create an empty array
$results = @()
FOREACH ($server in $servers) {
$Path = ‘\’ + $server.ServerName + '\c$\opscripts'
Write-Host $server
If (Test-Path $Path){
get-ChildItem $Path -Recurse |
ForEach-Object {
if ( Get-Content $_.FullName | Select-String -Pattern $SearchTerm) {
# Item was found
#Write-Output $_ | Select Line,LineNumber,Filename
#add results to the array
$results += @{
‘Server’ = $Server.ServerName
‘SearchTerm’ = $SearchTerm
‘FilePath’ = $.FullName
‘Line’ = $.Line
‘LineNumber’ = $_.LineNumber
}
}
}
}
}
#write the results array to the log file
results | Out-File $logPath -append
Output look like the below:
Server kalyantest
LineNumber
FilePath \kalyantest\c$\opscripts\old\20160216-adding…
Line
SearchTerm good
Server kalyantest
LineNumber
FilePath \kalyantest\c$\opscripts\IHS_DB_…
Line
SearchTerm good
Server kalyantest
LineNumber
FilePath \kalyantest\c$\opscripts\cpu_mem_Pittsbu…
Line
Any help would be appreciated. Thanks.
-Kalyan
ok, in order to help, in the future please use the instructions for denote code-blocks… and as Don Jones books tell you… formatting is important to readable code.
that aside,
the last line of the script:
results | Out-File $logPath -append
i believe this should read:
$results | Out-File $logPath -append
The problem is your misuse of the placeholder $_.
$_ is used to represent a pipeline input object.
You should be doing something like this:
$found = Get-Content $_.FullName | Select-String -Pattern $SearchTerm
results += @{
'Line' = $found.Line
'LineNumber' = $found.LineNumber
}