Availability Groups – Is Database Active

As part of administering SQL databases, I have quite a few PowerShell scripts to help automate the management and monitoring of databases and the server.  SQL 2012 introduced Always-On Availabilty Groups.  Always-On allows the creation of Availability Groups which is a logical collection of one or more databases.  The Availability Group allows you to have a database that exists on multiple servers at the same time, which is made up of a single read-write copy of the database (called the primary) and one or more read-only copies of the database (called secondaries).  In addition an Availability Group can be configured to use automatic failover so when the server running the primary becomes unavailable the database can automatically be brought up on one of the secondaries.

This adds a new wrinkle when running scripts because you do not always want to execute against both the primary and secondary copies of the database.  Instead you need to determine if a database is running on the primary and if it is do some action.

In PowerShell this is not a straight forward task, I ended up using SMO to determine if a database is active on the target server.

$serverName = "localhost"		# Hostname of the target server
$Server = new-object ("Microsoft.SqlServer.Management.Smo.Server") $serverName

$dbs = $Server.Databases		#Databases on the current server

# Loop Through each database and print if it is in primary
# or secondary. If a database is not in an availability group
# then the database status will not be printed.
$dbs | ForEach-Object {
    $targetAvg = $_.AvailabilityGroupName
    $dbName = $_.Name
    if ( $targetAvg )
    {
        if ( $Server.Name -eq ($Server.AvailabilityGroups | `
             Where-Object { $targetAvg -eq $_.Name }).PrimaryReplicaServerName )
        {
            "{0} is primary" -f $dbName
        }
        else
        {
            "{0} is secondary" -f $dbName
        }
    }
}

Ports Opened By Windows Firewall Rules

I recently came across an issue where I wasn’t sure if a specific set of ports were explicitly opened by the Windows Firewall.  As far as I could find there is not a simple method to display this in Windows.

After doing some digging I came up with a couple of PowerShell commands that queries the Windows Firewall to see which ports are opened by the firewall rules.

To list all ports opened by Windows Firewall rules execute the following in PowerShell:

 $fw = New-Object -ComObject HNetCfg.FWPolicy2
 $fw.Rules | where {$_.Enabled -like $true} | Format-Table LocalPorts

Find if a specific port is open in Windows Firewall and the rule that opens it via Powershell:

 $port = 808
 $fw = New-Object -ComObject HNetCfg.FWPolicy2
 $fw.Rules | where {$_.Enabled -like $true -and $_.LocalPorts -eq $port}

Posts

My goal will be to post one to three posts per week.  The focus of the posts will be tips and tricks I discover, how tos, and code.  Once in a while I may write up something happening in the industry that I have an opinion on.

Most of the code I write is Powershell, however I have a small side business that I am trying to get off the ground writing iOS applications.