0

The idea is to search for a pattern on several servers with Select-String and Invoke-Command.

I am not able to get the $results back to the local server correctly and print it either in a file and/or also in the console (this is not so important).

What I need is to be able to see the results of the search (filename, line, match)

Set Execution-Policy  RemoteSigned
$servidores = Get-Content "C:\ServerList.txt"
(Get-Date).ToString()

Write-Output "----------------Running script------------------"
Write-Output "---------------Servers in scope :---------------"
Write-Output $servidores
Write-Output "-----------------Starting Loop-----------------"

$ToExecute = {
Select-String -SimpleMatch "password" -Path C:\*.* -Exclude C:\Users\Public\resultados.txt
}


 foreach ($server in $servidores){
$result = Invoke-Command -ComputerName $server -ScriptBlock $ToExecute 
Write-Output "----------Executing Search on Server:-----------"
(Get-Date).ToString();$server; 
Write-Output "------------------------------------------------"
Write-Output $result
Out-File $result C:\Users\Public\resultados.txt 
}
Matt
  • 45,022
  • 8
  • 78
  • 119
Alexander Meise
  • 1,328
  • 2
  • 15
  • 31
  • You have two separate questions here. You should pick one of those and focus on it. Someone might know how to answer one but might be unwilling to answer both. Questions should single and clearly defined. – Matt Sep 15 '16 at 13:11
  • Alright - thanks for the advise - I will correct it then. – Alexander Meise Sep 15 '16 at 13:42
  • Please don't update the code in the question. It will make it harder for readers to understand what it going on. – Matt Sep 15 '16 at 14:30
  • Thanks Matt - I guess I need to learn how to post question first - i found the way anyway .. thanks - wanna point me to a stackoverflow How To? – Alexander Meise Sep 15 '16 at 14:39
  • [help]. It is also located in the upper right under Help. There is a lot to take in but most questions should be answered there. – Matt Sep 15 '16 at 14:43

2 Answers2

2

For Out-File if you are not piping, you will need to use the -inputobject flag. Because Out-File does not take the inputobject by position.

Out-File -InputObject $result -path C:\Users\Public\resultados.txt 

Otherwise you could use Tee-Object to replace the write-output/outfile.

$result | Tee -filepath C:\Users\Public\resultados.txt
BenH
  • 9,766
  • 1
  • 22
  • 35
0
Set Execution-Policy  RemoteSigned
$servidores = Get-Content "C:\ServerList.txt"
Write-Output ("----------------Running script-----"+(Get-Date).ToString()+"--  -----------")
Write-Output "--------------------------Servers in scope----------------------------"
Write-Output $servidores
foreach ($server in $servidores){
Write-Output ("---Executing Search on Server:---"+$server+"----at:"+(Get-    Date).ToString()+"-------------")
$result= Invoke-Command -ComputerName $server -ScriptBlock {Select-String -    Pattern "password" -Path C:\*.txt -AllMatches}
if ($result -ne $null){
Write-Output $result.ToString() 
}
}Read-Host -Prompt "Press Enter to exit"
Alexander Meise
  • 1,328
  • 2
  • 15
  • 31