Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Test AlwaysOn Application Read Intent Routing with PowerShell
##replace variables as needed##
$Server = 'AvailabilityGroupListener.fqdn.com'
$Database = 'DatabaseInsideAvailabilityGroup'
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "Server=$($Server);Database=$($Database);Integrated Security=True;MultiSubnetFailover=True"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText = 'Select @@SERVERNAME'
$Reader = $Command.ExecuteReader()
$Datatable = New-Object System.Data.DataTable
$Datatable.Load($Reader)
$Datatable
$Connection.Close()
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "Server=$($Server);Database=$($database);Integrated Security=True;MultiSubnetFailover=True;ApplicationIntent=ReadOnly"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText = 'Select @@SERVERNAME'
$Reader = $Command.ExecuteReader()
$Datatable = New-Object System.Data.DataTable
$Datatable.Load($Reader)
$Datatable
$Connection.Close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment