I have the below script, which runs an SQL query for each server in the list. I want it to add each SQL result to an array, and then once the ForEach loop is finished, I want it to print out the array as a formatted table.
At the moment, the formatted table is only showing me the results from the first server, my problem is making it work to show the results for all the servers.
$ServerNames=@('SQL1,6435'
,'SQL2,6431'
,'SQL3,6434'
,'SQL4,6438'
,'SQL5,6439')
$VersionQuery = "SELECT SERVERPROPERTY ('ServerName') [ServerName]`
, SERVERPROPERTY ('Edition') [Edition]`
, SERVERPROPERTY ('ProductLevel') [ProductLevel]`
, SERVERPROPERTY ('ProductUpdateLevel') [ProductUpdateLevel]`
, SERVERPROPERTY ('ProductBuildType') [ProductBuildType]`
, SERVERPROPERTY ('ProductUpdateReference') [ProductUpdateReference]`
, SERVERPROPERTY ('ProductVersion') [ProductVersion]"
$Results = foreach($ServerName in $ServerNames)
{
Invoke-Sqlcmd2 -Query $VersionQuery -QueryTimeout 5 -ConnectionTimeout 5 -ServerInstance $ServerName -ErrorAction Stop
}
$Results | ConvertTo-DbaDataTable | Format-Table
Can anyone guide me on achieving this?
Update: Removing ConvertTo-DbaDataTable
fixed the problem.