0

I need to get Availability Group and Listener name, concatenate both for a list of servers and then use it to get resource cluster.

What I've done so far:

foreach ($cluster in GC "D:\TEST\Servers_List_TEST.txt") 
{ 
$AGName = invoke-sqlcmd -Serverinstance $cluster -query "select left(name,25) as ' ' from sys.availability_groups" 
$LNName = invoke-sqlcmd -Serverinstance $cluster -query "select left(dns_name,25) as ' ' from sys.availability_group_listeners" 
$NetworkName = "$AGName_$LNName" 
Get-ClusterResource -cluster $cluster -name $NetworkName | Get-ClusterParameter HostRecordTTL,RegisterAllProvidersIP }

The main issue is on $NetworkName. It's returning System.data.DataRow, instead of concatenating $AGName_$LNName ( underscore is necessary between both ).

Nikhil Vartak
  • 5,002
  • 3
  • 26
  • 32
AdemirP
  • 113
  • 1
  • 3
  • 10

1 Answers1

1

Your main issue is that $AGName and its partner variable $LNName are System.Data.DataRow objects which is what Invoke-SQLcmd returns. They are not just simple strings. Since you are forcing them to strings PowerShell calls the ToString() method of those objects which, in this case, is just the object name.

You have also given that object a property of in your queries (which is just a space). The resulting object is using that property name.

You should add proper column names in your query but you will be able to pull out the relevant data by calling that property either way.

$NetworkName = "$($AGName." ")_$($LNName." ")" 

So using subexpressions we have gotten the value of the property [space]

Matt
  • 45,022
  • 8
  • 78
  • 119