如何使用 Powershell 远程 Select-串接多台服务器
How to Remote Select-String several servers with Powershell
想法是使用 Select-String 和 Invoke-Command 在多个服务器上搜索模式。
我无法将 $results 正确地返回到本地服务器并在控制台中的文件 and/or 中打印它(这不是那么重要)。
我需要的是能够看到搜索结果(文件名、行、匹配)
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
}
对于 Out-File,如果您不使用管道,则需要使用 -inputobject 标志。因为Out-File does not take the inputobject by position.
Out-File -InputObject $result -path C:\Users\Public\resultados.txt
否则您可以使用 Tee-Object 来替换 write-output/outfile。
$result | Tee -filepath C:\Users\Public\resultados.txt
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"
想法是使用 Select-String 和 Invoke-Command 在多个服务器上搜索模式。
我无法将 $results 正确地返回到本地服务器并在控制台中的文件 and/or 中打印它(这不是那么重要)。
我需要的是能够看到搜索结果(文件名、行、匹配)
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
}
对于 Out-File,如果您不使用管道,则需要使用 -inputobject 标志。因为Out-File does not take the inputobject by position.
Out-File -InputObject $result -path C:\Users\Public\resultados.txt
否则您可以使用 Tee-Object 来替换 write-output/outfile。
$result | Tee -filepath C:\Users\Public\resultados.txt
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"