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
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;
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With