I needed to make a simple housekeeping script checking 14 SQL Server's tables. I ended up using Adam's script from this question.
However when run, it skips posting results for a random amount of servers, and then dumps their queries in one table, then does any remaining servers individually. What have I done wrong? (Since I can't comment on Adam's answer)
I wouldn't mind collating all the data in a table and dump at the end, but then with columns for server name and result, if that is easier to accomplish.
This is my script, example output below;
$year = Get-Date -Format yyyy
$servers = gc 'ServerList.txt'
foreach ($server in $servers)
{
try
{
$ServerName = "$server"
$DatabaseName = "DatabaseName"
$Query = "select count(*) from Alarm_Activations where Activation_Date not like '%$year%'"
#$ds = $null -Tried adding this to fix the issue
#Timeout parameters
$QueryTimeout = 120
$ConnectionTimeout = 30
Write-Host ""
Write-Host ""
Write-Host " Checking $server "
#Action of connecting to the Database and executing the query and returning results if there were any.
$conn = New-Object System.Data.SqlClient.SQLConnection
$ConnectionString = "Data Source=$server\sqlexpress;Initial Catalog=DatabaseName;Connect Timeout=30; Integrated security=true;"
$conn.ConnectionString = $ConnectionString
$conn.Open()
$cmd = New-Object system.Data.SqlClient.SqlCommand($Query,$conn)
$cmd.CommandTimeout = $QueryTimeout
$ds = New-Object system.Data.DataSet
$da = New-Object system.Data.SqlClient.SqlDataAdapter($cmd)
[void]$da.fill($ds)
$conn.Close()
Write-Host "Number of activations older than $year :"
$ds.Tables
}
catch
{
Write-Error "Something went wrong. Seems you have to do it manually!"
return
}
}
Example output
The point of dumping the table seems random, but is usually past half the servers.
Earliest I've seen is Server-5, latest; Server-14;
& CheckAlarmActivation.ps1
Checking Server-1
Number of activations older than 2019 :
Checking Server-2
Number of activations older than 2019 :
Checking Server-3
Number of activations older than 2019 :
Checking Server-4
Number of activations older than 2019 :
Checking Server-5
Number of activations older than 2019 :
Checking Server-6
Number of activations older than 2019 :
Checking Server-7
Number of activations older than 2019 :
Checking Server-8
Number of activations older than 2019 :
Checking Server-9
Number of activations older than 2019 :
Checking Server-10
Number of activations older than 2019 :
Column1
-------
0
0
0
3318
1069
4375
8
9357
74
1735
Checking Server-11
Number of activations older than 2019 :
7917
Checking Server-12
Number of activations older than 2019 :
3583
Checking Server-13
Number of activations older than 2019 :
5622
Checking Server-14
Number of activations older than 2019 :
4166