search
Categories
Sponsors
VirtualMetric Hyper-V Monitoring, Hyper-V Reporting
Archive
Blogroll

Badges
MCSE
Community

Cozumpark Bilisim Portali
Getting MSSQL Server Availability Group Listeners via PowerShell
Posted in Windows Powershell | 1 Comment | 5,066 views | 11/05/2016 15:42

You can get detailed info about SQL Server Availability Group Listeners via following code:

1
2
3
4
5
6
7
8
9
10
$MSSQLServerManager = New-Object 'Microsoft.SqlServer.Management.SMO.Server'
 
# Get SQL Availability Group Listener
$HostSQLAvailabilityGroupListener = @($MSSQLServerManager.AvailabilityGroupListeners)[0]
 
# Get SQL Availability Group Listener Properties
$HostSQLName = $HostSQLAvailabilityGroupListener.Name;
$HostSQLAvailabilityGroupListenerGuid = $HostSQLAvailabilityGroupListener.UniqueId;
$HostSQLIPAddresses = $HostSQLAvailabilityGroupListener.AvailabilityGroupListenerIPAddresses;
$HostSQLPortNumber = $HostSQLAvailabilityGroupListener.PortNumber;

You can also check other properties by listing all properties of $HostSQLAvailabilityGroupListener.


Comments (1)

John A Rolstead

January 16th, 2021
16:14:50

replace this:
@($MSSQLServerManager.AvailabilityGroupListeners)[0]

with:
@($MSSQLServerManager.AvailabilityGroups.AvailabilityGroupListeners)[0]

but to me it is more clear to use . to progress thru the objects. when I get an object, I pass it to get-member to see available properties. then progress through like this:

# Get SQL Server Manager object
$MSSQLServerManager = New-Object ‘Microsoft.SqlServer.Management.SMO.Server’

# Get SQL Availability Group object
$HostSQLAvailabilityGroups = $MSSQLServerManager.AvailabilityGroups

# Get SQL Availability Group Listener object
$HostSQLAvailabilityGroupListener = $HostSQLAvailabilityGroups.AvailabilityGroupListeners

# Get SQL Availability Group Listener Properties
$HostSQLAvailabilityGroupListenerName = $HostSQLAvailabilityGroupListener.Name;
$HostSQLAvailabilityGroupListenerGuid = $HostSQLAvailabilityGroupListener.UniqueId;
$HostSQLAvailabilityGroupListenerPort = $HostSQLAvailabilityGroupListener.PortNumber;

# Get SQL Listener IP Properties
$HostSQLAGListenerIPs = $HostSQLAvailabilityGroupListener.AvailabilityGroupListenerIPAddresses
$HostSQLIPAddress = $HostSQLAGListenerIPs.IPAddress;
$HostSQLSubnetMask = $HostSQLAGListenerIPs.SubnetMask;



Leave a Reply