How to Read Error Log on All Servers (e.g.,hundreds) using Powershell?


# Option 1 - read error logs for all instances on a server
PS SQLSERVER:\sql\xu-acer-pc> $a=dir
PS SQLSERVER:\sql\xu-acer-pc> $a.readErrorLog()
PS SQLSERVER:\sql\xu-acer-pc> $a.readErrorLog()|Out-file "C:\users\xu-acer\documents\errorlogs.cvs"


# Option 2 - read error logs for all instances on a server

PS SQLSERVER:\sql\xu-acer-pc> $a= dir sqlserver:\sql\Xu-acer-pc
PS SQLSERVER:\sql\xu-acer-pc> $a
<#
Instance Name
-------------
DEFAULT
I2
#>

PS SQLSERVER:\sql\xu-acer-pc> $a | %{$_.ReadErrorLog()} ############## ReadErrorLog ( ) without a parameter read all current logs
PS SQLSERVER:\sql\xu-acer-pc> $a | %{$_.InstanceName; $_.ReadErrorLog()} #output the instance name


# Option 3 - read error logs for the selected instances on a server
PS SQLSERVER:\sql\xu-acer-pc> $a=dir |? {$_.InstanceName -eq "I2" }
PS SQLSERVER:\sql\xu-acer-pc> $a.readErrorLog()|Out-file "C:\users\xu-acer\documents\errorlogs_I2.csv"

# Option 4 - what if you want to read all error logs including the current and archived ones for all instances on a server

$a=dir sqlserver:\sql\localhost

$a | % {
        $_.InstanceName;
        $Logs=$_.EnumErrorLogs();
        ForEach ($log in $Logs)
            {
                write-host ("=====The name for the cuurent log, by default, is 0 =============================")
                $_.name
                $log.name
                write-host ("=================================================================================")
            
                $_.ReadErrorLog($log.Name) # note ReadErrorLog has an overload method with one parameter
            }


        }