- Function GetSCOMGroupsInSQL ($DBServer, $DBCatalog)
- {
- $SqlServer = $DBServer
- $SqlCatalog = $DBCatalog
- $SQLQuery = "select distinct SCOMGroup = convert(varchar(100), upper(rtrim(ltrim(BME.Displayname))))"
- $SQLQuery += " from BaseManagedEntity BME"
- $SQLQuery += " left join dbo.ManagedTypeView T1 on (BME.BaseManagedEntityId = T1.Id)"
- $SQLQuery += " Left join dbo.Relationship R1 on(BME.BaseManagedEntityId=R1.Sourceentityid)"
- $SQLQuery += " left join BaseManagedEntity ME2 on (ME2.BaseManagedEntityId=R1.targetentityid)"
- $SQLQuery += " left join dbo.ManagedTypeView T2 on(ME2.BaseManagedEntityId=T2.id)"
- $SQLQuery += " where (T1.Name like 'UINameSpace%.Group' or T1.Name like '%MySpecificGroupName%')"
- $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
- $sqlConnection.ConnectionString = "Server = $sqlserver; Database = $sqlcatalog; Integrated Security = True"
- $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
- $SqlCmd.CommandText = $SqlQuery
- $SqlCmd.Connection = $SqlConnection
- $SQlCmd.CommandTimeout = 120
- $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
- $SqlAdapter.SelectCommand = $SqlCmd
- $DSet = New-Object System.Data.DataSet
- $SqlAdapter.Fill($DSet) | out-null
- $SqlConnection.Close()
- $DSet.Tables[0]
- }
How to use it :
- # --------- Read the SCOMDB Server and Catalog
- $SCOMServer = $Config.Groups.SCOMDB.SCOMDBServerName
- $SCOMCatalog = $Config.Groups.SCOMDB.SCOMDBName
- # --------- Query SCOM DB to get Agent list and Group List
- $GroupList = GetSCOMGroupsInSQL $SCOMServer $SCOMCatalog
The script wil return a table $GroupList that can also be re-used.
This posting is provided "AS IS" with no warranties.
No comments:
Post a Comment