The aim was to have a very fast answer of the script and using the Get-Agent cmdlet was really too long.
- Function GetSCOMAgentsInSQL ($DBServer, $DBCatalog)
- {
- $SqlServer = $DBServer
- $SqlCatalog = $DBCatalog
- $SQLQuery = "SELECT distinct(B.displayname)"
- $SQLQuery += " FROM TypedManagedEntity T WITH(NOLOCK)"
- $SQLQuery += " JOIN BaseManagedEntity B WITH(NOLOCK) ON B.BaseManagedEntityId = T.BaseManagedEntityId"
- $SQLQuery += " WHERE T.ManagedTypeId = dbo.fn_ManagedTypeId_MicrosoftSystemCenterAgent()"
- $SQLQuery += " order by 1"
- $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
- $AgentList = GetSCOMAgentsInSQL $SCOMServer $SCOMCatalog
The script wil return a table $AgentList that can also be re-used.
This posting is provided "AS IS" with no warranties.
No comments:
Post a Comment