Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get primary replica info for the SQL availability group by powershell

I want to get the machine of current primary replica in Powershell. Is there cmdlet to do it?

I load sqlps module and dump the find all sql command, but seems no one is related to this..

Thanks

like image 898
user1504146 Avatar asked Nov 14 '25 11:11

user1504146


2 Answers

I think this should work:

$null = [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo");

$svr = New-Object Microsoft.SqlServer.Management.Smo.Server("AServerInstanceInYourAG");

$svr.AvailabilityGroups["YourAvailabilityGroup"].PrimaryReplicaServerName;
like image 139
muhmud Avatar answered Nov 17 '25 08:11

muhmud


You can also get the Primary Replica by connecting to the listener

$null = [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo");

$listenerName = "YourListenerName";

$sqlListener = New-Object Microsoft.SqlServer.Management.Smo.Server($listenerName);

$primaryReplicaName = $sqlListener.AvailabilityGroups.PrimaryReplicaServerName;
like image 36
Gary L Fry Avatar answered Nov 17 '25 10:11

Gary L Fry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!