Powershell script to search specific error on all log files in given servers



$searchpattern =  "Error I want to search in log file"

$Servers = @("MYWEBAPP42","MYWEBAPP43","MYWEBAPP60","MYWEBAPP61")


foreach ($server in $servers)
{
    $mypath = "\\" + $server + "\M$\SQLDBA\MyToolPath\LogFolder"
    


    $myfiles=Get-ChildItem $mypath | where {($_.CreationTime -ge $(Get-Date).AddHours(-120))}

    foreach ($file in $myfiles)
    {
         IF ( Get-Content $file.FullName | Select-String -Pattern  $searchpattern )
        {
            Write-Host $file.FullName
       }
    } 
}

#

I have used this code to find specific activity failure during a critical process which is running across 25 different servers.
this script also searches the log files which are created in last 5 days.

This entry was posted in Powershell, Productivity and tagged , , , , , . Bookmark the permalink.

Leave a comment