Here's the main part of the PowerShell script.
$databases = @("devDB.serverName.com/devTest")
foreach($database in $databases) {
#Query to find any failed login attempts yesterday
$query = "SELECT * FROM error_log WHERE Text LIKE 'Login failed%'"
#Standard connection string object
$connectionString = New-Object System.Data.Sqlclient.Sqlconnection("Data Source="+$database+";Initial Catalog=ErrorReports;Integrated Security=True;")
#Standard adapter object
$adapter = New-Object System.Data.Sqlclient.Sqldataadapter($query, $connectionString)
#Standard Datatable object
$dataTable = New-Object System.Data.Datatable
#Try to execute the query and store it in a datatable
$adapter.Fill($dataTable) | Out-Null
}
And here's the error I kept getting. The PowerShell script is run on the same machine that the sql server instances are installed. The user who logged in to run the PowerShell script has local administrator rights and SQL Server Sysadmin permission.
There was a problem connecting to and/or querying devDB.serverName.com/devTest for the failed SQL Server login attempts. Specific error:
Exception calling "Fill" with "1" argument(s): "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"