How to connect each SQL instance to get SQL server version updated into the temp table? -- Trying to get SQL Server verion with CMS information. Run this in SQLCMD mode

-- Create temp table for Instance
CREATE TABLE #Instance_list(Server_Group_Name nvarchar(12), Server_Name nvarchar(50), Instance_Description nvarchar(512))
GO
-- Get Instance list from CMS
INSERT INTO #Instance_list 
SELECT CONVERT(nvarchar(12), groups.name) AS Server_Group_Name
     ,CONVERT(nvarchar(50), svr.server_name) AS Server_Name
     , CONVERT(nvarchar(512),svr.description) as Instance_Description

FROM msdb.dbo.sysmanagement_shared_server_groups_internal groups 
INNER JOIN msdb.dbo.sysmanagement_shared_registered_servers_internal svr
ON groups.server_group_id = svr.server_group_id 
GO
-- Add SQL version column
ALTER TABLE  #Instance_list ADD Instance_Version nvarchar(512)
-- Loop each instance
DECLARE @Instance nvarchar(50)
DECLARE Instance_List_cursor CURSOR FOR 
SELECT [Server_Name] from #Instance_list
OPEN Instance_List_cursor

 FETCH NEXT FROM Instance_List_cursor 
 INTO @Instance

 WHILE @@FETCH_STATUS = 0
 BEGIN
 -- How to connect each SQL instance to get SQL server version updated in to the temp table?
PRINT @Instance
DECLARE @SQLVERSION nvarchar(512)
--:Connect @Instance

SET @SQLVERSION = (SELECT @@VERSION FROM sys.dm_os_sys_info)
PRINT @SQLVERSION
--:CONNECT CMS_INSTANCE
-- UPDATE #Instance_list SET [Instance_Version] = @SQLVERSION WHERE Server_Name = @Instance

 FETCH NEXT FROM Instance_List_cursor INTO @Instance
 END 

CLOSE Instance_List_cursor
DEALLOCATE Instance_List_cursor

SELECT * FROM #Instance_list
GO


DROP TABLE #Instance_list


How to connect each SQL instance to get SQL server version updated into the temp table?

Thanks in advanced.
Just use dbatools - Get-DbaCmsRegServerGroup


  Gets list of Server Groups objects stored in SQL Server Central Management Server (CMS).


If you have multiple servers to connect, you can use foreach to loop through each servers and store the result into a variable.

You can even write to a database using ConvertTo-DbaDataTable and Write-DbaDataTable