Author Posts

January 1, 2012 at 12:00 am

by mg48 at 2013-01-31 12:10:38

How does Powershell handle connecting to a mirrored failover partner automatically as it will in a .Net program that specifies the failover partner in the connection string?

by Klaas at 2013-02-01 00:28:02

I think you should check the status of the databases, the principal will have status 'normal', the mirror is 'restoring'. Of course there's a possibility neither is available.
If ((Get-Childitem SQLSERVER:SQL\server1\instance1\databases | Where { $ -LIKE 'mirroreddbname'}).status -eq 'Normal') { $instance = "server1\instance1" }
ElseIf ((Get-Childitem SQLSERVER:SQL\server2\instance2\databases | Where { $ -LIKE 'mirroreddbname'}).status -eq 'Normal') { $instance = "server2\instance2" }
Else { "None of the instances is available" }
You can do pretty much the same with SMO instead of the SQL Provider.

It 's probably even better to check the availability of the server first, to catch any case where the principal is unavailable for other reasons than a failover.