Need to create Powershell report for SQL Always on secondary serves.

Parag Kambli 0 Reputation points
2024-03-28T19:56:55.6533333+00:00

Hello All,
A strange request.

We are running multiple always on servers.

Backups takes place on secondary replica of always on server.

I need to create a report in powershell or t-sql that would identify secondary replica and verify backup status.

DM1-- instance name

M1-- primary instance

M2 --secondary instance

$sqlservers = @( 
     'DM1',
     'M2',
     'M3')
$dbrecords = @() ## array for query results
Foreach($server in $sqlservers) {
   $dbrecords += invoke-sqlcmd -query 'DROP TABLE IF EXISTS #mysgbackupstatus1;
DROP TABLE IF EXISTS ##mysgbackupstatus;


CREATE TABLE #mysgbackupstatus1(
replica_server_name nvarchar(256),
role_desc nvarchar(60))
-----below query writing only secondary replica to #mysgbackupstatus1 table 
INSERT INTO #mysgbackupstatus1 (replica_server_name, role_desc) 
SELECT replica_server_name
from sys.dm_hadr_availability_replica_states as ha
inner join sys.availability_replicas as a on  ha.replica_id=a.replica_id
WHERE role_desc = ''SECONDARY'';


Each row in #mysgbackupstatus represents secondary SQL instance.

I am failing here as I do not know how to connect to SQL instance by fetching row from #mysgbackupstatus

once connected I am planning to run below CTE into a CSV file.

   $dbrecords += invoke-sqlcmd -query ';with backup_cte as(    select        database_name,        backup_type =            case type                when ''D'' then ''database''                when ''L'' then ''log''                when ''I'' then ''differential''                else ''other''            end,        backup_finish_date,        rownum =             row_number() over            (                partition by database_name, type                 order by backup_finish_date desc            )    from msdb.dbo.backupset)' -ServerInstance $server}$dbrecords | Export-Csv "C:\Temp\SQLOutput.csv"## $dbrecords | Out-GridView

I am messed up and do not know if what I am talking is feasible or not.

Please help and guide.
I am rookie in world of development.

Thank you in advance.


SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,716 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,364 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Erland Sommarskog 101K Reputation points MVP
    2024-03-28T22:06:13.8433333+00:00

    I am not sure that I understand. But that if you are connecting to many servers as suggested by the Foreach loop, you are creating a temp table on each server. And when you disconnect from that server to move on to the next one, the temp table is dropped. You need to return the data to the PowerShell script with a query and then save that to some data structure in the PowerShell script. (Or write to a table on a master server.)

    For an example of how to capture the data, see Example 7 in the documentation for Invoke-Sqlcmd.

    1 person found this answer helpful.