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

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


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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s