Quantcast
Channel: SQLServerCentral » Programming » Powershell » Latest topics
Viewing all articles
Browse latest Browse all 467

Extract output from central management servers into a table

$
0
0
This one has my head spinning. I would like to run a query on all of the servers registered in the central management server and then write the output to a table on our management server. I can get the output and also load it into the database but what is loaded is not right. Server Name and Node Name don't go into a table correctly. I've been reading a bunch of examples from Google but really don't understand them. Somehow I need to split the output into two parts so one column can go into ServerName and one into NodeName.Any help would be greatly appreciated.[code="plain"]#Get list of servers in CMS$instanceNameList = invoke-Sqlcmd -query "SELECT [server_name] as NameFROM [msdb].[dbo].[sysmanagement_shared_registered_servers_internal] SSRSIjoin [msdb].[dbo].[sysmanagement_shared_server_groups_internal] SSSGIon SSRSI.server_group_id = SSSGI.server_group_idwhere SSRSI.server_group_id = '11'" -serverinstance "TESTSRV2"#Collect server name and node node from each into a variableforeach($instanceName in $instanceNameList){$results += Invoke-Sqlcmd -Query "SELECT @@SERVERNAME as ServerName, SERVERPROPERTY('ComputerNamePhysicalNetBIOS') as NodeName" -ServerInstance $instanceName.Name}#Insert the results into the STG_Results table in the DBA database$Connection = New-Object System.Data.SQLClient.SQLConnection$Connection.ConnectionString = "server='TESTSRV2';database='DBA';trusted_connection=true;"$Connection.Open()$Command = New-Object System.Data.SQLClient.SQLCommand$Command.Connection = $Connectionforeach($node in $results){ $sql = "insert into STG_Results (ServerName,NodeName) select '$node.ServerName','$node.NodeName'" $Command.CommandText = $sql $Command.ExecuteNonQuery()}$Connection.Close()[/code]

Viewing all articles
Browse latest Browse all 467

Trending Articles