# Finding string on remote server

**URL:** <https://forums.powershell.org/t/finding-string-on-remote-server/7054>\
**Category:** PowerShell Help\
**Created:** [August 23, 2016, 11:54am UTC](https://forums.powershell.org/t/finding-string-on-remote-server/7054 "2016-08-23T11:54:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![venkata-kalyan](https://avatars.discourse-cdn.com/v4/letter/v/b3f665/32.png) [@venkata-kalyan](https://forums.powershell.org/u/venkata-kalyan)\
**Post date:** [August 23, 2016, 11:54am UTC](https://forums.powershell.org/t/finding-string-on-remote-server/7054/1 "2016-08-23T11:54:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [August 23, 2016, 12:40pm UTC](https://forums.powershell.org/t/finding-string-on-remote-server/7054/2 "2016-08-23T12:40:21Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![venkata-kalyan](https://avatars.discourse-cdn.com/v4/letter/v/b3f665/32.png) [@venkata-kalyan](https://forums.powershell.org/u/venkata-kalyan)\
**Post date:** [August 23, 2016, 1:17pm UTC](https://forums.powershell.org/t/finding-string-on-remote-server/7054/3 "2016-08-23T13:17:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![david-schmidtberger](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@david-schmidtberger](https://forums.powershell.org/u/david-schmidtberger)\
**Post date:** [August 23, 2016, 3:03pm UTC](https://forums.powershell.org/t/finding-string-on-remote-server/7054/4 "2016-08-23T15:03:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [August 23, 2016, 3:34pm UTC](https://forums.powershell.org/t/finding-string-on-remote-server/7054/5 "2016-08-23T15:34:38Z")

</div>

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
}
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:40pm UTC](https://forums.powershell.org/t/finding-string-on-remote-server/7054/6 "2024-05-16T20:40:53Z")

</div>


