ConfigMgr 2007 Site Hierarchy Discovery

Microsoft.SystemCenter.ConfigurationManager.2007.Site.Hierarchy.Discovery (Discovery)

Discover the ConfigMgr 2007 Site Hierarchy.

Knowledge Base article:

Summary

Discovers and populates the ConfigMgr 2007 Site Hierarchy Diagram.

Element properties:

TargetMicrosoft.SystemCenter.ConfigurationManager.2007.Microsoft_SMSv4_Providers_Installation
EnabledTrue
Frequency86400
RemotableFalse

Object Discovery Details:

Discovered Classes and their attribuets:

Member Modules:

ID Module Type TypeId RunAs 
DiscoveryDataSource DataSource System.CommandExecuterDiscoveryDataSource Default

Source Code:

<Discovery ID="Microsoft.SystemCenter.ConfigurationManager.2007.Site.Hierarchy.Discovery" Enabled="onStandardMonitoring" Remotable="false" Target="Microsoft.SystemCenter.ConfigurationManager.2007.Microsoft_SMSv4_Providers_Installation">
<Category>Discovery</Category>
<DiscoveryTypes>
<DiscoveryClass TypeID="Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_Class">
<Property TypeID="Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_Class" PropertyID="Version_1"/>
<Property TypeID="Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_Class" PropertyID="Site_System_Type"/>
<Property TypeID="Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_Class" PropertyID="Components"/>
<Property TypeID="Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_Class" PropertyID="Site_Code_1"/>
<Property TypeID="Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_Class" PropertyID="Server_Type"/>
<Property TypeID="Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_Class" PropertyID="Name_1"/>
<Property TypeID="Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_Class" PropertyID="Database_Server_Type"/>
</DiscoveryClass>
<DiscoveryClass TypeID="Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Site_Hierarchy"/>
<DiscoveryClass TypeID="Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Site">
<Property TypeID="Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Site" PropertyID="SiteCode"/>
<Property TypeID="Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Site" PropertyID="SiteName"/>
<Property TypeID="Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Site" PropertyID="Version"/>
</DiscoveryClass>
<DiscoveryClass TypeID="Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Central_Site"/>
<DiscoveryClass TypeID="Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Primary_Site"/>
<DiscoveryClass TypeID="Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Secondary_Site"/>
</DiscoveryTypes>
<DataSource ID="DiscoveryDataSource" TypeID="SystemLibrary!System.CommandExecuterDiscoveryDataSource">
<IntervalSeconds>86400</IntervalSeconds>
<ApplicationName>%windir%\System32\cscript.exe</ApplicationName>
<WorkingDirectory/>
<CommandLine>$file/ConfigMgr2007SiteHierarchyDiscovery.vbs$ 0 $MPElement$ $Target/Id$ $Target/Host/Property[Type="WindowsLibrary!Microsoft.Windows.Computer"]/NetworkName$</CommandLine>
<TimeoutSeconds>1800</TimeoutSeconds>
<RequireOutput>true</RequireOutput>
<Files>
<File>
<Name>ConfigMgr2007SiteHierarchyDiscovery.vbs</Name>
<Contents><Script>
'*******************************************************************************
' Script Name - ConfigMgr 2007 Site Hierarchy Discovery
'
' Purpose - Discover the SMS Site Hierarchy down from the current primary
' site server. SMS 2003 Servers are ignored.
'
' The following additional events can be raised:
'
' 1100 - An event used only for debugging or tracing.
' 1101 - Script executed successfully.
' 1102 - An error occurred in executing this script.
' 1105 - Accessed denied due to connection failure or permissions.
'
' Assumptions - This script only runs on a system with the SMS WMI Provider
' installed. The system can therefore be either an SMS Primary
' Site Server or Remote Provider.
'
' This script does not support agentless mode.
'
' Parameters - SourceType Must be 0 for Discovery.
' SourceID Discover ID that is equal to the GUID for
' Microsoft.SystemCenter.ConfigurationManager.
' 2007.Site.Hierarchy.Discovery.
' ManagedEntityId The class, the object, that this script is
' to run against. Must be the GUID for
' Microsoft.SystemCenter.ConfigurationManager.
' 2007.Microsoft_SMSv4_Providers_Installation
' TargetComputerFQDN The FQDN of the computer on which this
' script is executing.
'
' Change Hist - Date Version Description
' -------- --------------- -----------
' 04/21/04 02.50.0175.0000 Created
' 05/06/04 02.50.0177.0000 Improved tracing
' 07/08/04 05.0.2705.0000 Corrected error reporting in event.
' 02/19/07 05.0.3132.0000 Changed GetFQNetbiousName to
' GetFQDomainName since SCOM 2007
' requires it.
' 03/12/07 6.0.4946.0000 Changes for discovery execution and
' new SCOM 2007 topology diagram.
' Changed name of script form SMS 2003
' Topology Discovery - Server to SMS
' 2003 Site Hierarchy Discovery.
' 05/28/07 6.0.5000.0000 Discover the local site if SMS 2003
' SP3 or later and any SMS 2003 child
' sites reporting to it. SP3 sites
' will support specifying server FQDNs.
' 07/23/07 6.0.5000.1 Return an empty discovery packet if
' nothing was found for the local site.
' 08/10/07 6.0.5000.0 Ported from SMS 2003 and updated.
' 08/18/07 6.0.5000.2 Added event tracing support.
'
' (c) Copyright 2007, Microsoft Corp., All Rights Reserved
'*******************************************************************************

Option Explicit


'Event Severity Constants
'========================

Const EVENT_TYPE_SUCCESS = 0
Const EVENT_TYPE_ERROR = 1
Const EVENT_TYPE_WARNING = 2
Const EVENT_TYPE_INFORMATION = 4

Const EVENTLOG_AUDIT_SUCCESS = 8
Const EVENTLOG_AUDIT_FAILURE = 16


'Event Number Constants
'======================

Const EVENT_ID_NOTANEVENT = 1100
Const EVENT_ID_SCRIPTSUCCESS = 1101
Const EVENT_ID_SCRIPTERROR = 1102
Const EVENT_ID_ACCESSDENIED = 1105


'Discovery Constants
'===================

Const SITE_SERVER_TO_SITE_SERVER_ARROW_STYLE = "None"
Const SITE_SERVER_TO_SITE_SERVER_CONNECTOR_TYPE = "Site Server to Site Server"
Const SITE_SERVER_TO_SITE_SERVER_LINE_COLOR = "Black"
Const SITE_SERVER_TO_SITE_SERVER_LINE_STYLE = "Solid"
Const SITE_SERVER_TO_SITE_SERVER_LINE_WEIGHT = "2"

Const SITE_SERVER_TO_SITE_SYSTEM_ARROW_STYLE = "None"
Const SITE_SERVER_TO_SITE_SYSTEM_CONNECTOR_TYPE = "Site Server to Site System"
Const SITE_SERVER_TO_SITE_SYSTEM_LINE_COLOR = "Blue"
Const SITE_SERVER_TO_SITE_SYSTEM_LINE_STYLE = "Dash"
Const SITE_SERVER_TO_SITE_SYSTEM_LINE_WEIGHT = "1"


'Debug level constants
'=====================

Const DBG_TRACE = 1
Const DBG_WARNING = 2
Const DBG_ERROR = 3
Const DBG_NONE = 4


'Other constants
'===============

Const SCRIPT_NAME = "ConfigMgr 2007 Site Hierarchy Discovery"

Const DISPLAY_NAME_SMS_SITE_HIERARCHY = "ConfigMgr Site Hierarchy"
Const DISPLAY_NAME_SMS_SITE = "ConfigMgr Site"
Const DISPLAY_NAME_SMS_SERVER = "ConfigMgr Server"

Const SMS_SITE_TYPE_SECONDARY = 1
Const SMS_SITE_TYPE_PRIMARY = 2

Const NUMBER_OF_REQUIRED_INPUT_PARAMETERS = 4

Const E_INVALIDARG = &amp;H80070057


'Global variables
'================

Dim g_oMomScriptAPI
Dim g_oDiscData


'Local variables
'===============

Dim oArgs

Dim lError

Dim bEventTracing

Dim strError
Dim strSourceType
Dim strSourceID
Dim strManagedEntityId
Dim strTargetComputerFQDN


On Error Resume Next


LogMessage DBG_TRACE, SCRIPT_NAME &amp; " script starting at local time: " &amp; CStr(Time)

'This script must run on a computer with MOM scripting support.
'==============================================================

LogMessage DBG_TRACE, "Creating MOM Scripting API object."

Set g_oMomScriptAPI = CreateObject("Mom.ScriptAPI")

If Err &lt;&gt; 0 Then
lError = Err.number
strError = GetErrorString(Err)
LogMessage DBG_ERROR, "Failed to create MOM Scripting API object." &amp; strError
Wscript.Quit lError
End If

'Check if event tracing is configured. If it is, raise a start event now
'that the MOM Scripting object has been created.
'========================================================================

bEventTracing = IsEventTracingEnabled()

If (bEventTracing = True) Then
LogEvent EVENT_ID_NOTANEVENT, EVENT_TYPE_INFORMATION, "started at local time: " &amp; CStr(Time)
End If

'Verify and get command line arguments.
'======================================

LogMessage DBG_TRACE, "Checking command line arguments."

Set oArgs = WScript.Arguments

if oArgs.Count &lt;&gt; NUMBER_OF_REQUIRED_INPUT_PARAMETERS Then
Err.Raise E_INVALIDARG
LogScriptError "Failed to receive the required number of arguments."
oArgs = Nothing
g_oMomScriptAPI = Nothing
Wscript.Quit E_INVALIDARG
End If

strSourceType = oArgs(0)
strSourceID = oArgs(1)
strManagedEntityId = oArgs(2)
strTargetComputerFQDN = oArgs(3)

'Verify and get command line arguments.
'======================================

LogMessage DBG_TRACE, "Creating discovery data object."

Set g_oDiscData = g_oMomScriptAPI.CreateDiscoveryData(strSourceType, strSourceID, strManagedEntityId)

If Err &lt;&gt; 0 Then
lError = Err.number
LogScriptError "Failed to create discovery data object."
g_oDiscData = Nothing
oArgs = Nothing
g_oMomScriptAPI = Nothing
Wscript.Quit lError
End If

'Discover the SMS Site Hierarchy down from this primary site.
'============================================================

DoTopologyDiscovery

lError = Err.number

g_oDiscData = Nothing
oArgs = Nothing
g_oMomScriptAPI = Nothing

If lError = 0 Then
Err.Clear
WScript.Echo ""
LogMessage DBG_TRACE, "Successfully performed topology discovery."
Else
Err.Raise lError
WScript.Echo ""
LogScriptError "Failed to perform topology discovery."
Wscript.Quit lError
End If

If (bEventTracing = True) Then
LogEvent EVENT_ID_SCRIPTSUCCESS, EVENT_TYPE_INFORMATION, "completed at local time: " &amp; CStr(Time)
End If

LogMessage DBG_TRACE, SCRIPT_NAME &amp; " script completed at local time: " &amp; CStr(Time)


'******************************************************************************
' Name: DoTopologyDiscovery
'
' Purpose: Performs all discovery topology functions.
'
' Parameters: None
'
' Returns: Nothing
'
Sub DoTopologyDiscovery()

Dim oService
Dim oSiteInstance
Dim oSiteHierarchyInstance

Dim strSiteCode
Dim strSiteServerName
Dim strSiteServerFQDomainName


On Error Resume Next


LogMessage DBG_TRACE, "Beginning topology discovery tasks."

'Connect to the SMS site namespace.
'==================================

Set oService = GetConnectionToSite(strSiteCode)

If Err Then
LogScriptError "Failed to connect to the WMI SMS site namespace for this computer."
Exit Sub
End If

'Refresh the Site Control File for this session with the SMS Provider
'so we do not use the default copy.
'====================================================================

RefreshSiteControlFile oService, strSiteCode

If Err Then
LogScriptError "Failed to refresh the Site Control File for this session with the SMS WMI Provider on this computer."
Exit Sub
End If

'Create the singleton class instance for the SMS Site Hierarchy.
'This instance should not be added to the discovery data object.
'===============================================================

Set oSiteHierarchyInstance = g_oDiscData.CreateClassInstance("$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Site_Hierarchy']$")
'g_oDiscData.AddInstance oSiteHierarchyInstance

If Err Then
LogScriptError "Failed to create the SMS Site Hierarchy class instance."
Set oService = Nothing
Exit Sub
End If

'Discover the local site, the site that this server is a member of.
'This will be an SCCM 2007 primary site, RTM or later. If there is an
'error, do not exit but attempt to discovery any existing child sites.
'The error may be due to a server without a specified FQDNs.
'=====================================================================

DoSiteTopologyDiscovery oService, _
oSiteHierarchyInstance, _
strSiteCode, _
oSiteInstance, _
strSiteServerName, _
strSiteServerFQDomainName

If Err Then
LogScriptError "Failed to do site topology discovery of the local site."
'Set oService = Nothing
'Exit Sub
End If

If IsEmpty(oSiteInstance) Then
LogMessage DBG_TRACE, "Nothing was found by site topology discovery for the local site."
LogMessage DBG_TRACE, "Submitting empty Discovery Data packet."
g_oMomScriptAPI.Return g_oDiscData
Set oService = Nothing
Exit Sub
End If

'Discover child sites. If a child site is a secondary site, discover
'all topology instances for it.
'====================================================================

DoChildSiteTopologyDiscovery oService, _
strSiteCode, _
oSiteInstance, _
strSiteServerName, _
strSiteServerFQDomainName

If Err Then
LogScriptError "Failed to do child site topology discovery for local site."
'Set oService = Nothing
'Exit Sub
End If

'Submit the Discovery Data object.
'=================================

LogMessage DBG_TRACE, "Submitting Discovery Data packet."

g_oMomScriptAPI.Return g_oDiscData

Set oService = Nothing

End Sub


'******************************************************************************
' Name: DoSiteTopologyDiscovery
'
' Purpose: Performs all discovery topology functions for the local site.
'
' Parameters: oService The Wbem Services object for the local
' provider's site.
' oSiteHierarchyInstance Instance of the SMS_Site_Hierarchy class.
' strSiteCode Site code for the local SMS site.
' oSiteInstance Instance of a class derived from
' SMS_Site.
' strSiteServerName Netbios name of the SMS site server.
' strSiteServerFQDomainName The fully qualified domain name of the
' SMS site server.
'
' Returns: oSiteInstance, strSiteServerName, strSiteServerFQDomainName
'
Sub DoSiteTopologyDiscovery(oService, _
oSiteHierarchyInstance, _
strSiteCode, _
oSiteInstance, _
strSiteServerName, _
strSiteServerFQDomainName)

Dim oSite
Dim oSiteSet

Dim strQuery


On Error Resume Next


'Check input parameters.
'=======================

If IsEmpty(oService) Or _
IsEmpty(oSiteHierarchyInstance) Or _
IsEmpty(strSiteCode) Then
Err.Raise E_INVALIDARG
LogScriptError "Invalid parameter passed to DoSiteTopologyDiscovery."
Exit Sub
End If

'Discover the local site, the site that this server is a member of.
'This will be a primary site.
'
'Query for the current site information.
'==================================================================

LogMessage DBG_TRACE, "Discovering the topology for site '" &amp; strSiteCode &amp; "'."

strQuery = GetSMSSSiteQuery(strSiteCode)

Set oSiteSet = oService.ExecQuery(strQuery)

If Not IsEmpty(oSiteSet) And (oSiteSet.Count = 1) Then

For Each oSite In oSiteSet

If Not IsEmpty(oSite) Then

'Create and add to the discovery data object an SMS_Site
'class instance for this site.
'=======================================================

AddSiteInstance oService, oSite, oSiteInstance

If Err Then
LogScriptError "Failed to create SMS_Site class instance."
Exit For
End If

'If the site is a central site, create and add to the
'discovery data object an SMS_Site_Hierarchy containment
'relationship instance for this site. Note that the
'ReportingSiteCode property is not checked since it is
'used to specify the beginning of the hierarchy displayed
'in the Admin UI and consequently will be a null string
'even though the site is not a central site.
'========================================================

If (GetParentSiteCode(oService, strSiteCode) = "") Then

AddSiteHierarchyContainsSiteInstance oSiteHierarchyInstance, oSiteInstance

If Err Then
LogScriptError "Failed to create SMS_Site_Hierarchy containment relationship instance."
Exit For
End If

End If

'Get the site server's name and fully qualified domain
'name.
'=====================================================

strSiteServerName = oSite.ServerName

strSiteServerFQDomainName = GetFQDomainName(oService, _
strSiteCode, _
strSiteServerName)

If Err Then
LogScriptError "Failed to get the FQDN for site server '" &amp; strSiteServerName &amp; "'."
Exit For
End If

'For each server in this site create and add to the
'discovery data object a containment relationship
'instance for it with the site.
'==================================================

AddSiteContainsServerInstances oService, _
oSiteInstance, _
strSiteCode, _
strSiteServerName, _
strSiteServerFQDomainName

If Err Then
LogScriptError "Failed to add Site to Server containment relationship instances."
Exit For
End If

'For each site system in this site create and add to the
'discovery data object an SMS Server-SMS Server Connector
'relationship instance. Each site system will have a
'connector relationship with the site server.
'========================================================

AddSiteServerToSiteSystemInstances oService, _
strSiteCode, _
strSiteServerName, _
strSiteServerFQDomainName

If Err Then
LogScriptError "Failed to add Site Server to Site System Instances."
Exit For
End If

Else
LogScriptError "Failed get site information for site " &amp; strSiteCode &amp; "."
Exit For
End If

Next

Set oSiteSet = Nothing

Else

'Failed to query for the current site information or there is no
'site information because this site's version is unsupported.
'===============================================================

If Err Then
LogScriptError "Failed to query site information for site " &amp; strSiteCode &amp; "."
Else
LogMessage DBG_TRACE, "Site '" &amp; strSiteCode &amp; "' is an unsupported version."
End If

End If

End Sub


'******************************************************************************
' Name: DoChildSiteTopologyDiscovery
'
' Purpose: Performs all discovery topology functions for the child sites of
' the local site.
'
' Parameters: oService The Wbem Services object for the local
' provider's site.
' strSiteCode Site code for the local SMS site.
' oSiteInstance Instance of a class derived from
' SMS_Site representing the local site.
' strSiteServerName Netbios name of the SMS site server for
' the local site.
' strSiteServerFQDomainName The fully qualified domain name of the
' SMS site server for the local site.
' If not specified, site server to site
' server discovery will fail but all
' other discoveries will be attempted.
'
' Returns: Nothing
'
Sub DoChildSiteTopologyDiscovery(oService, _
strSiteCode, _
oSiteInstance, _
strSiteServerName, _
strSiteServerFQDomainName)

Dim oChildSite
Dim oChildSiteSet

Dim strChildSiteCode
Dim strChildSiteServerName
Dim strChildSiteServerFQDomainName

Dim strError
Dim strQuery

Dim oChildSiteInstance


On Error Resume Next


'Check input parameters.
'=======================

If IsEmpty(oService) Or _
IsEmpty(strSiteCode) Or _
IsEmpty(oSiteInstance) Or _
IsEmpty(strSiteServerName) Then
Err.Raise E_INVALIDARG
LogScriptError DBG_TRACE, "Invalid parameter passed to DoChildSiteTopologyDiscovery."
Exit Sub
End If

'Discover child sites. If a child site is a secondary site, discover
'all topology instances for it.
'
'Query for the site information of all sites reporting to the current
'site.
'====================================================================

LogMessage DBG_TRACE, "Discovering the topology for child sites of '" &amp; strSiteCode &amp; "'."

strQuery = GetReportingSMSSSiteQuery(strSiteCode)

Set oChildSiteSet = oService.ExecQuery(strQuery)

If Not IsEmpty(oChildSiteSet) Then

For Each oChildSite In oChildSiteSet

If Not IsEmpty(oChildSite) Then

'Create and add to the discovery data object an SMS_Site
'class instance for this site.
'=======================================================

AddSiteInstance oService, oChildSite, oChildSiteInstance

If Err Then
LogScriptError "Failed to create SMS_Site class instance for child site."
Exit For
End If

'Add site to site instances for the child site.
'==============================================

strChildSiteCode = oChildSite.SiteCode

AddSiteToSiteInstances strSiteCode, _
oSiteInstance, _
strChildSiteCode, _
oChildSiteInstance

If Err Then
LogScriptError "Failed to add site to site instances."
Exit For
End If

'Get the child site server's name and fully qualified
'domain name. If the FQDN can not be retrieved, the
'FQDN may be present for any of the SMS server in this
'site so go to the next site and complete at least the
'site level discovery.
'=====================================================

strChildSiteServerName = oChildSite.ServerName

strChildSiteServerFQDomainName = GetFQDomainName(oService, _
strChildSiteCode, _
strChildSiteServerName)

If Err Then
LogScriptError "Failed to get the FQDN for child site server '" &amp; strChildSiteServerName &amp; "'."
Else

'Add a site server to site server connector instance.
'====================================================

AddSiteServerToSiteServerInstance strSiteServerName, _
strSiteServerFQDomainName, _
strChildSiteServerName, _
strChildSiteServerFQDomainName

If Err Then
LogScriptError "Failed to add Site Server to Site Server instance."
'Exit For
End If

'If the child site is a secondary site then add instances
'for its site server and each site system.
'========================================================

If oChildSite.Type = SMS_SITE_TYPE_SECONDARY Then

LogMessage DBG_TRACE, "Site '" &amp; strChildSiteCode &amp; _
"' is a secondary site so discovery it."

'For each server in this site create and add to the
'discovery data object a containment relationship
'instance for it with the site.
'==================================================

AddSiteContainsServerInstances oService, _
oChildSiteInstance, _
strChildSiteCode, _
strChildSiteServerName, _
strChildSiteServerFQDomainName

If Err Then
LogScriptError "Failed to add Site to Server containment relationship instances for child site."
Exit For
End If

'For each site system in this site create and add to the
'discovery data object an SMS Server-SMS Server Connector
'relationship instance. Each site system will have a
'connector relationship with the site server.
'========================================================

AddSiteServerToSiteSystemInstances oService, _
strChildSiteCode, _
strChildSiteServerName, _
strChildSiteServerFQDomainName

If Err Then
LogScriptError "Failed to add Site Server to Site System Instances for child site."
Exit For
End If

End If 'Is secondary site

End If 'Failed to get GetFQDomainName

End If 'oChildSite is not empty

Next 'Next child site in returned set

Set oChildSiteSet = Nothing

End If 'oChildSiteSet is not emtpy

End Sub


'******************************************************************************
' Name: GetConnectionToSite
'
' Purpose: Gets the Wbem Services object for the local provider's site.
'
' Parameters: strSiteCode The SMS site that the local provider supports.
'
' Returns: Object The object to return or nothing
' strSiteCode The SMS Site Code
'
Function GetConnectionToSite(strSiteCode)


Dim oWbemService
Dim oWbemLocator

Dim strMachine

Dim strQuery


On Error Resume Next


'Get the SMS Provider information for the provider that is installed
'on this machine.
'===================================================================

GetLocalSMSProviderInfo strMachine, strSiteCode

If Err Then
Set GetConnectionToSite = Nothing
Exit Function
End If

'Connect to the site's SMS namespace through the locally installed
'provider. This will simplify the security model since the MOM Agent
'running under Local System will always be able to access this
'provider. The provider will then know how to connect to the
'appropriate SMS database.
'====================================================================

Set oWbemLocator = CreateObject("WbemScripting.SWbemLocator")

If Err Then
Set GetConnectionToSite = Nothing
Exit Function
End If

Set oWbemService = oWbemLocator.ConnectServer(strMachine, "root\sms\site_" &amp; strSiteCode)

If Err Then
Set GetConnectionToSite = Nothing
Exit Function
End If

Set GetConnectionToSite = oWbemService

'Cleanup
'=======

Set oWbemLocator = Nothing
Set oWbemService = Nothing


End Function


'******************************************************************************
' Name: GetLocalSMSProviderInfo
'
' Purpose: Gets the SMS Provider information for the local provider. If the
' current machine is an SMS Site Server, this information will be
' for this machine's SMS site. If the current machine is a Remote
' Provider and SQL Server that is shared, it will not be.
'
' Parameters: strMachine The machine that the provider is installed on,
' the current machine.
' strSiteCode The SMS site that the provider supports.
'
' Returns: strMachine, strSiteCode
'
Sub GetLocalSMSProviderInfo(strMachine, strSiteCode)

Dim oService
Dim oProviderLocation
Dim oProviderLocationSet

Dim strQuery
Dim strCurrentMachine


On Error Resume Next


'Get this machine's name.
'========================

strCurrentMachine = GetCurrentSystem()

If Err Then
Exit Sub
End If

'Connect to the WMI SMS namespace for this machine.
'==================================================

Set oService = GetObject("winmgmts:root\sms")

If Err Then
Exit Sub
End If

'Query for the SMS Provider location information for the current
'machine, the locally installed SMS Provider.
'===============================================================

strQuery = "select * from SMS_ProviderLocation where Machine='" &amp; strCurrentMachine &amp; "'"

Set oProviderLocationSet = oService.ExecQuery(strQuery)

If Not IsEmpty(oProviderLocationSet) Then

For Each oProviderLocation In oProviderLocationSet

strMachine = oProviderLocation.Machine
strSiteCode = oProviderLocation.SiteCode

Next

Set oProviderLocationSet = Nothing

End If

Set oService = Nothing

End Sub


'******************************************************************************
' Name: GetCurrentSystem
'
' Purpose: Gets the name of the current system.
'
' Parameters: None
'
' Returns: String, current system name
'
Function GetCurrentSystem()

Dim oComputerSystem
Dim oComputerSystemSet

Dim strQuery


On Error Resume Next


'Initialize return value.
'========================

GetCurrentSystem = Empty

'Get the instances of computer system under the WMI CIMV2 namespace.
'There will be only one for the current system.
'===================================================================

Set oComputerSystemSet = GetObject("winmgmts:").InstancesOf("Win32_ComputerSystem")

If Err Then
Exit Function
End If

If Not IsEmpty(oComputerSystemSet) Then

For Each oComputerSystem In oComputerSystemSet

GetCurrentSystem = oComputerSystem.Name

Next

Set oComputerSystemSet = Nothing

End If

End Function


'******************************************************************************
' Name: RefreshSiteControlFile
'
' Purpose: Refreshes the Site Control File for the current session with the
' SMS Provider i.e. oService.
'
' Parameters: oService The Wbem Services object for the local provider's
' site.
' strSiteCode The SMS Site of interest.
'
' Returns: Nothing
'
Sub RefreshSiteControlFile(oService, strSiteCode)


Dim oSiteControlFile

Dim strReturnValue


On Error Resume Next


'Check input parameters.
'=======================

If IsEmpty(oService) Or _
IsEmpty(strSiteCode) Then
Err.Raise E_INVALIDARG
LogScriptError "Invalid parameter passed to RefreshSiteControlFile."
Exit Sub
End If

'Call the RefreshSCF method on the SMS_SiteControlFile class.
'============================================================

Set oSiteControlFile = oService.Get("SMS_SiteControlFile")

If Err Then
LogScriptError "Failed to get SMS_SiteControlFile class."
Exit Sub
End If

strReturnValue = oSiteControlFile.RefreshSCF(strSiteCode)

If Err Then
LogScriptError "Failed to call the RefreshSCF method on the SMS_SiteControlFile class; return value = " &amp; strReturnValue &amp; "."
Set oSiteControlFile = Nothing
Exit Sub
End If

'Cleanup
'=======

Set oSiteControlFile = Nothing

End Sub


'******************************************************************************
' Name: GetSMSSSiteQuery
'
' Purpose: Gets query string for SMS Site information for the specified SMS
' Site. Filtering is on SCCM 2007 RTM Escrow or later sites only.
'
' Parameters: strSiteCode The SMS site that the provider supports, this
' computer's SMS Site.
'
' Returns: String, a query
'
Function GetSMSSSiteQuery(strSiteCode)

GetSMSSSiteQuery = "select * " &amp; _
"from SMS_Site " &amp; _
"where (SiteCode='" &amp; strSiteCode &amp; "') " &amp; _
"and ((Version like ""4.0%"") and (BuildNumber &gt;= 5924))"

End Function


'******************************************************************************
' Name: GetReportingSMSSSiteQuery
'
' Purpose: Gets query string for SMS Site information for all SMS Sites
' reporting to the specified site. Filtering is on SCCM 2007
' sites only.
'
' Parameters: strSiteCode The SMS site that the provider supports, this
' computer's SMS Site.
'
' Returns: String, a query
'
Function GetReportingSMSSSiteQuery(strSiteCode)

GetReportingSMSSSiteQuery = "select * " &amp; _
"from SMS_Site " &amp; _
"where (ReportingSiteCode='" &amp; strSiteCode &amp; "') " &amp; _
"and (Version like ""4.0%"")"

End Function


'******************************************************************************
' Name: GetSMSSSiteSystemSummarizerQuery
'
' Purpose: Gets query string for SMS site systems for the specified site
' exlcuding its site server.
'
' Parameters: strSiteCode The SMS Site of interest.
' strSiteServer The Site Server for the specified site.
'
' Returns: String, a query
'
Function GetSMSSSiteSystemSummarizerQuery(strSiteCode, strSiteServer)

'Note the query should be distinct so that multiple instances for the
'same machine in the SMS Site System Summarizer table are not returned.
'======================================================================

GetSMSSSiteSystemSummarizerQuery = "select distinct " &amp; _
"res.ServerName " &amp; _
"from SMS_SiteSystemSummarizer as summ " &amp; _
"join SMS_SystemResourceList as res " &amp; _
"on summ.SiteCode=res.SiteCode " &amp; _
"and summ.Role=res.RoleName " &amp; _
"where (summ.SiteCode='" &amp; strSiteCode &amp; "') " &amp; _
"and not (res.ServerName='" &amp; strSiteServer &amp; "')"

End Function


'******************************************************************************
' Name: GetParentSiteCodeQuery
'
' Purpose: Gets query string for a site's parent site code. Filtering is on
' the specified site.
'
' Parameters: strSiteCode The SMS Site of interest.
'
' Returns: String, a query
'
Function GetParentSiteCodeQuery(strSiteCode)

GetParentSiteCodeQuery = "select " &amp; _
"ParentSiteCode " &amp; _
"from SMS_SCI_SiteDefinition " &amp; _
"where (SiteCode='" &amp; strSiteCode &amp; "')"

End Function


'******************************************************************************
' Name: GetServerFQDNQuery
'
' Purpose: Gets query string for a server's FQDN. Filtering is on the
' specified site and server name.
'
' Parameters: strSiteCode The SMS Site of interest.
' strServer The Netbios name of the server in the specified
' site.
'
' Returns: String, a query
'
Function GetServerFQDNQuery(strSiteCode, strServer)

GetServerFQDNQuery = "select " &amp; _
"ServerRemoteName " &amp; _
"from SMS_SystemResourceList " &amp; _
"where (SiteCode='" &amp; strSiteCode &amp; "') " &amp; _
"and (ServerName='" &amp; strServer &amp; "')"

End Function


'******************************************************************************
' Name: GetParentSiteCode
'
' Purpose: Returns the site code of the parent for the specified site.
'
' Parameters: oService The Wbem Services object for the local provider's
' site.
' strSiteCode The SMS Site of interest.
'
' Returns: String, a site code.
'
Function GetParentSiteCode(oService, strSiteCode)

Dim strQuery

Dim oSite
Dim oSiteSet


On Error Resume Next


'Check input parameters.
'=======================

If IsEmpty(oService) Or _
IsEmpty(strSiteCode) Then
Err.Raise E_INVALIDARG
LogScriptError "Invalid parameter passed to GetParentSiteCode."
Exit Function
End If

'Query for only the parent site code for the specified site.
'===========================================================

strQuery = GetParentSiteCodeQuery(strSiteCode)

Set oSiteSet = oService.ExecQuery(strQuery)

If Not IsEmpty(oSiteSet) Then

For Each oSite In oSiteSet

If Not IsEmpty(oSite) Then

GetParentSiteCode = oSite.ParentSiteCode
Exit For

End If

Next

Set oSiteSet = Nothing

End If

End Function


'******************************************************************************
' Name: GetFQDomainName
'
' Purpose: Returns the fully qualified domain name for a server in the
' specified site.
'
' Parameters: oService The Wbem Services object for the local provider's
' site.
' strSiteCode The SMS Site of interest.
' strServer The Netbios name of the SMS server in the specified
' site.
'
' Returns: String, the fully qualified domain name.
'
Function GetFQDomainName(oService, strSiteCode, strServer)

Dim strQuery

Dim oServerFQDN
Dim oServerFQDNSet


On Error Resume Next


'Check input parameters.
'=======================

If IsEmpty(oService) Or _
IsEmpty(strSiteCode) Or _
IsEmpty(strServer) Then
Err.Raise E_INVALIDARG
LogScriptError "Invalid parameter passed to GetFQDomainName."
Exit Function
End If

'Query for only the server in the specified site.
'================================================

strQuery = GetServerFQDNQuery(strSiteCode, strServer)

Set oServerFQDNSet = oService.ExecQuery(strQuery)

If Not IsEmpty(oServerFQDNSet) Then

For Each oServerFQDN In oServerFQDNSet

If Not IsEmpty(oServerFQDN) Then
GetFQDomainName = AppendFQDN(oServerFQDN.ServerRemoteName)
Exit For

End If

Next

Set oServerFQDNSet = Nothing

End If

End Function

'******************************************************************************
' Name: AppendFQDN
'
' Purpose: Returns the fully qualified domain name .
'
' Parameters:
' strServer The Netbios name of the SMS server in the specified
' site.
'
' Returns: String, the fully qualified domain name.
Function AppendFQDN(strServerFQDN)

Dim oADSystemInfo
Dim domainName
Dim tmpServerDNS
Dim cmpResult

set oADSystemInfo = CreateObject("ADSystemInfo")
domainName = UCase(oADSystemInfo.ForestDNSName)

if Len(domainName) &gt; Len(strServerFQDN) then
strServerFQDN = strServerFQDN &amp; "." &amp; domainName
Else
tmpServerDNS = right(strServerFQDN , Len(domainName))
cmpResult = StrComp(UCase(tmpServerDNS) , domainName)

if cmpResult &lt;&gt; 0 then
strServerFQDN = strServerFQDN &amp; "." &amp; domainName
End if
End if

AppendFQDN = strServerFQDN
End Function

'******************************************************************************
' Name: AddSiteInstance
'
' Purpose: Creates an instance of the SMS_Central_Site, SMS_Primary_Site or
' SMS_Secondary_Site class and adds it to the discovery data object.
'
' Parameters: oService The Wbem Services object for the local
' provider's site.
' oSite The site to create a discovery instance for.
' oSiteInstance The created instance.
'
' Returns: oSiteInstance
'
Sub AddSiteInstance(oService, oSite, oSiteInstance)


On Error Resume Next


'Check input parameters.
'=======================

If IsEmpty(oService) Or _
IsEmpty(oSite) Or _
((oSite.Type &lt;&gt; SMS_SITE_TYPE_SECONDARY) And (oSite.Type &lt;&gt; SMS_SITE_TYPE_PRIMARY)) Then
Err.Raise E_INVALIDARG
LogScriptError "Invalid parameter passed to AddSiteInstance."
Exit Sub
End If

'Create, initialize and add an instance of the appropriate SMS_Site
'derived class to the discovery data object.
'==================================================================

If (oSite.Type = SMS_SITE_TYPE_SECONDARY) Then

LogMessage DBG_TRACE, "Adding SMS_Secondary_Site class instance for site '" &amp; oSite.SiteCode &amp; "'."
Set oSiteInstance = g_oDiscData.CreateClassInstance("$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Secondary_Site']$")

Else

If (oSite.Type = SMS_SITE_TYPE_PRIMARY) Then

If (GetParentSiteCode(oService, oSite.SiteCode) = "") Then
LogMessage DBG_TRACE, "Adding SMS_Central_Site class instance for site '" &amp; oSite.SiteCode &amp; "'."
Set oSiteInstance = g_oDiscData.CreateClassInstance("$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Central_Site']$")
Else
LogMessage DBG_TRACE, "Adding SMS_Primary_Site class instance for site '" &amp; oSite.SiteCode &amp; "'."
Set oSiteInstance = g_oDiscData.CreateClassInstance("$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Primary_Site']$")
End If

End If

End If

oSiteInstance.AddProperty "$MPElement[Name='SystemLibrary!System.Entity']/DisplayName$", DISPLAY_NAME_SMS_SITE &amp; " - " &amp; oSite.SiteName
oSiteInstance.AddProperty "$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Site']/SiteCode$", oSite.SiteCode
oSiteInstance.AddProperty "$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Site']/SiteName$", oSite.SiteName
oSiteInstance.AddProperty "$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Site']/Version$", oSite.Version
g_oDiscData.AddInstance oSiteInstance

End Sub



'******************************************************************************
' Name: AddSiteToSiteInstances
'
' Purpose: Creates instances for the site to site relationships.
'
' Parameters: strSiteCode Site code for the parent SMS site.
' oSiteInstance An instance of the specified parent site.
' strChildSiteCode Site code for the child SMS site.
' oSiteInstance An instance of the specified child site.
'
' Returns: Nothing
'
Sub AddSiteToSiteInstances(strSiteCode, _
oSiteInstance, _
strChildSiteCode, _
oChildSiteInstance)

Dim oRelationshipInstance


On Error Resume Next


'Check input parameters.
'=======================

If IsEmpty(strSiteCode) Or _
IsEmpty(oSiteInstance) Or _
IsEmpty(strChildSiteCode) Or _
IsEmpty(oChildSiteInstance) Then
Err.Raise E_INVALIDARG
LogScriptError "Invalid parameter passed to AddSiteToSiteInstances."
Exit Sub
End If

LogMessage DBG_TRACE, "Adding site to site instances for site '" &amp; _
strSiteCode &amp; "' and '" &amp; strChildSiteCode &amp; "'."

'Create and add the containment relationship instance that the
'specified site has with the specified child site.
'=============================================================

LogMessage DBG_TRACE, "Adding SMS_Site contains SMS_Site relationship instance."

Set oRelationshipInstance = g_oDiscData.CreateRelationshipInstance("$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Site.Contains.Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Site']$")
oRelationshipInstance.Source = oSiteInstance
oRelationshipInstance.Target = oChildSiteInstance
g_oDiscData.AddInstance oRelationshipInstance

End Sub


'******************************************************************************
' Name: AddSiteHierarchyContainsSiteInstance
'
' Purpose: Creates and adds the specified SMS Site Hierarchy to SMS Server
' containment relationship to the discovery data object.
'
' Parameters: oSiteHierarchyInstance The SMS_Site_Hierarchy class instance.
' oSiteInstance An SMS_Site class instance.
'
' Returns: Nothing
'
Sub AddSiteHierarchyContainsSiteInstance(oSiteHierarchyInstance, oSiteInstance)

Dim oRelationshipInstance


On Error Resume Next


'Check input parameters.
'=======================

If IsEmpty(oSiteHierarchyInstance) Or _
IsEmpty(oSiteInstance) Then
Err.Raise E_INVALIDARG
LogScriptError "Invalid parameter passed to AddSiteHierarchyContainsSiteInstance."
Exit Sub
End If

LogMessage DBG_TRACE, "Adding site hierarchy containment relationsip for site."

'Create and add the containment relationship instance that this server
'has with the specified site.
'=====================================================================

Set oRelationshipInstance = g_oDiscData.CreateRelationshipInstance("$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Site_Hierarchy.Contains.Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Central_Site']$")
oRelationshipInstance.Source = oSiteHierarchyInstance
oRelationshipInstance.Target = oSiteInstance
g_oDiscData.AddInstance oRelationshipInstance

End Sub


'******************************************************************************
' Name: AddSiteContainsServerInstance
'
' Purpose: Creates and adds the specified SMS Site to SMS Server containment
' relationship to the discovery data object.
'
' Parameters: oSiteInstance An SMS_Site class instance.
' strServerName Netbios name of an SMS Server.
' strServerNameFQDomainName The fully qualified domain name of
' an SMS server.
'
' Returns: Nothing
'
Sub AddSiteContainsServerInstance(oSiteInstance, _
strServerName, _
strServerNameFQDomainName)

Dim oServerInstance
Dim oRelationshipInstance


On Error Resume Next


'Check input parameters.
'=======================

If IsEmpty(oSiteInstance) Or _
IsEmpty(strServerName) Or _
IsEmpty(strServerNameFQDomainName) Then
Err.Raise E_INVALIDARG
LogScriptError "Invalid parameter passed to AddSiteContainsServerInstance."
Exit Sub
End If

LogMessage DBG_TRACE, "Adding site containment relationsip for server " &amp; strServerName &amp; "."

'Create and add an SMS Server class instance for the specified server.
'This will not be a complete discovery object as server discovery
'will do this since it runs locally on the computer.
'=====================================================================

Set oServerInstance = g_oDiscData.CreateClassInstance("$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_Class']$")
oServerInstance.AddProperty "$MPElement[Name='SystemLibrary!System.Entity']/DisplayName$", DISPLAY_NAME_SMS_SERVER &amp; " - " &amp; strServerName
oServerInstance.AddProperty "$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_Class']/Name_1$", strServerName
oServerInstance.AddProperty "$MPElement[Name='WindowsLibrary!Microsoft.Windows.Computer']/PrincipalName$", strServerNameFQDomainName
g_oDiscData.AddInstance oServerInstance

'Create and add the containment relationship instance that this server
'has with the specified site.
'=====================================================================

Set oRelationshipInstance = g_oDiscData.CreateRelationshipInstance("$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Site.Contains.Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_Class']$")
oRelationshipInstance.Source = oSiteInstance
oRelationshipInstance.Target = oServerInstance
g_oDiscData.AddInstance oRelationshipInstance

End Sub


'******************************************************************************
' Name: AddSiteContainsServerInstances
'
' Purpose: Creates and adds the SMS Site to SMS Server containment
' relationships to the discovery data object.
'
' Parameters: oService The Wbem Services object for the
' local provider's site.
' oSiteInstance The instance of the specified site.
' strSiteCode The site code an SMS site.
' strSiteServerName The Netbios name of an SMS site
' server.
' strSiteServerFQDomainName The fully qualified domain name of
' an SMS site server.
'
' Returns: Nothing
'
Sub AddSiteContainsServerInstances(oService, _
oSiteInstance, _
strSiteCode, _
strSiteServerName, _
strSiteServerFQDomainName)

Dim oSiteSystemSummarizer
Dim oSiteSystemSummarizerSet

Dim strError
Dim strQuery
Dim strSiteSystemName
Dim strSiteSystemFQDomainName


On Error Resume Next


'Check input parameters.
'=======================

If IsEmpty(oService) Or _
IsEmpty(oSiteInstance) Or _
IsEmpty(strSiteCode) Or _
IsEmpty(strSiteServerName) Or _
IsEmpty(strSiteServerFQDomainName) Then
Err.Raise E_INVALIDARG
LogScriptError "Invalid parameter passed to AddSiteContainsServerInstances."
Exit Sub
End If

LogMessage DBG_TRACE, "Discovering servers that are members of site '" &amp; strSiteCode &amp; "'."


'Create and add a containment relationship to the data discovery
'object for the site and site server.
'===============================================================

AddSiteContainsServerInstance oSiteInstance, _
strSiteServerName, _
strSiteServerFQDomainName

If Err Then
LogScriptError "Failed to add Site to Site Server containment relationship instance."
Exit Sub
End If

'Create and add a containment relationships to the data discovery
'object for the site and each site system.
'===============================================================

strQuery = GetSMSSSiteSystemSummarizerQuery(strSiteCode, strSiteServerName)

Set oSiteSystemSummarizerSet = oService.ExecQuery(strQuery)

If Not IsEmpty(oSiteSystemSummarizerSet) Then

For Each oSiteSystemSummarizer In oSiteSystemSummarizerSet

If Not IsEmpty(oSiteSystemSummarizer) Then

strSiteSystemName = oSiteSystemSummarizer.ServerName

strSiteSystemFQDomainName = GetFQDomainName(oService, _
strSiteCode, _
strSiteSystemName)

AddSiteContainsServerInstance oSiteInstance, _
strSiteSystemName, _
strSiteSystemFQDomainName

If Err Then
LogScriptError "Failed to add Site to Site System containment relationship instance."
Exit For
End If

End If

Next

Set oSiteSystemSummarizerSet = Nothing

End If

End Sub


'******************************************************************************
' Name: AddSiteServerToSiteServerInstance
'
' Purpose: Adds a site server to site server instance to the specified
' connector relationship collection.
'
' Parameters: strSiteServerName The Netbios name of the parent
' SMS site server.
' strSiteServerFQDomainName The fully qualified domain
' name of the parent SMS site
' server.
' strChildSiteServerName The Netbios name of a child SMS
' site server.
' strSiteServerFQDomainName.
' strChildSiteServerFQDomainName The fully qualified domain
' name of a child SMS site server.
'
' Returns: Nothing
'
Sub AddSiteServerToSiteServerInstance(strSiteServerName, _
strSiteServerFQDomainName, _
strChildSiteServerName, _
strChildSiteServerFQDomainName)

Dim oSiteServerInstance
Dim oChildSiteServerInstance
Dim oRelationshipInstance


On Error Resume Next


'Check input parameters.
'=======================

If IsEmpty(strSiteServerName) Or _
IsEmpty(strSiteServerFQDomainName) Or _
IsEmpty(strChildSiteServerName) Or _
IsEmpty(strChildSiteServerFQDomainName) Then
Err.Raise E_INVALIDARG
LogScriptError "Invalid parameter passed to AddSiteServerToSiteServerInstance."
Exit Sub
End If

LogMessage DBG_TRACE, "Adding connection from site server " &amp; strSiteServerFQDomainName &amp; _
" to child site server " &amp; strChildSiteServerFQDomainName &amp; "."

'Create and add an SMS_Server class instance for the specified parent
'site server. This will not be a complete discovery object as server
'discovery will do this since it runs locally on the computer.
'====================================================================

Set oSiteServerInstance = g_oDiscData.CreateClassInstance("$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_Class']$")
oSiteServerInstance.AddProperty "$MPElement[Name='SystemLibrary!System.Entity']/DisplayName$", DISPLAY_NAME_SMS_SERVER &amp; " - " &amp; strSiteServerName
oSiteServerInstance.AddProperty "$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_Class']/Name_1$", strSiteServerName
oSiteServerInstance.AddProperty "$MPElement[Name='WindowsLibrary!Microsoft.Windows.Computer']/PrincipalName$", strSiteServerFQDomainName
g_oDiscData.AddInstance oSiteServerInstance

'Create and add an SMS_Server class instance for the specified child
'site server. This will not be a complete discovery object as server
'discovery will do this since it runs locally on the computer.
'====================================================================

Set oChildSiteServerInstance = g_oDiscData.CreateClassInstance("$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_Class']$")
oChildSiteServerInstance.AddProperty "$MPElement[Name='SystemLibrary!System.Entity']/DisplayName$", DISPLAY_NAME_SMS_SERVER &amp; " - " &amp; strChildSiteServerName
oChildSiteServerInstance.AddProperty "$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_Class']/Name_1$", strChildSiteServerName
oChildSiteServerInstance.AddProperty "$MPElement[Name='WindowsLibrary!Microsoft.Windows.Computer']/PrincipalName$", strChildSiteServerFQDomainName
g_oDiscData.AddInstance oChildSiteServerInstance

'Create, initialize and add an instance of the SMS Server-SMS Server
'Connector relationship class. An SMS Server is identified by its
'SMS Server Name, its Netbios Name.
'===================================================================

Set oRelationshipInstance = g_oDiscData.CreateRelationshipInstance("$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_SMS_Server_Connector_Relationship']$")

oRelationshipInstance.Source = oSiteServerInstance
oRelationshipInstance.Target = oChildSiteServerInstance

oRelationshipInstance.AddProperty "$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_SMS_Server_Connector_Relationship']/ArrowStyle$", SITE_SERVER_TO_SITE_SERVER_ARROW_STYLE
oRelationshipInstance.AddProperty "$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_SMS_Server_Connector_Relationship']/Connector_Name$", strSiteServerFQDomainName &amp; " - " &amp; strChildSiteServerFQDomainName
oRelationshipInstance.AddProperty "$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_SMS_Server_Connector_Relationship']/Connector_Type$", SITE_SERVER_TO_SITE_SERVER_CONNECTOR_TYPE
oRelationshipInstance.AddProperty "$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_SMS_Server_Connector_Relationship']/LineColor$", SITE_SERVER_TO_SITE_SERVER_LINE_COLOR
oRelationshipInstance.AddProperty "$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_SMS_Server_Connector_Relationship']/LineStyle$", SITE_SERVER_TO_SITE_SERVER_LINE_STYLE
oRelationshipInstance.AddProperty "$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_SMS_Server_Connector_Relationship']/LineWeight$", SITE_SERVER_TO_SITE_SERVER_LINE_WEIGHT

g_oDiscData.AddInstance oRelationshipInstance

End Sub


'******************************************************************************
' Name: AddSiteServerToSiteSystemInstance
'
' Purpose: Adds a site server to site system instance to the specified
' connector relationship.
'
' Parameters: oSiteServerInstance SMS_Server class instance for the
' site server.
' strSiteServerFQDomainName The fully qualified domain name
' of the SMS site server.
' strSiteSystemName The Netbios name of an SMS site
' server.
' strSiteSystemFQDomainName The fully qualified domain name of
' an SMS site system.
'
' Returns: Nothing
'
Sub AddSiteServerToSiteSystemInstance(oSiteServerInstance, _
strSiteServerFQDomainName, _
strSiteSystemName, _
strSiteSystemFQDomainName)

Dim oSiteSystemInstance
Dim oRelationshipInstance


On Error Resume Next


'Check input parameters.
'=======================

If IsEmpty(oSiteServerInstance) Or _
IsEmpty(strSiteSystemName) Or _
IsEmpty(strSiteSystemFQDomainName) Then
Err.Raise E_INVALIDARG
LogScriptError DBG_TRACE, "Invalid parameter passed to AddSiteServerToSiteSystemInstance."
Exit Sub
End If

LogMessage DBG_TRACE, "Adding connection from site server " &amp; strSiteServerFQDomainName &amp; _
" to site system " &amp; strSiteSystemFQDomainName &amp; "."

'Create and add an SMS_Server class instance for the specified site
'system. This will not be a complete discovery object as server
'discovery will do this since it runs locally on the computer.
'==================================================================

Set oSiteSystemInstance = g_oDiscData.CreateClassInstance("$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_Class']$")
oSiteSystemInstance.AddProperty "$MPElement[Name='SystemLibrary!System.Entity']/DisplayName$", DISPLAY_NAME_SMS_SERVER &amp; " - " &amp; strSiteSystemName
oSiteSystemInstance.AddProperty "$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_Class']/Name_1$", strSiteSystemName
oSiteSystemInstance.AddProperty "$MPElement[Name='WindowsLibrary!Microsoft.Windows.Computer']/PrincipalName$", strSiteSystemFQDomainName
g_oDiscData.AddInstance oSiteSystemInstance

'Create, initialize and add an instance of the SMS Server-SMS Server
'Connector relationship class. An SMS Server is identified by its
'SMS Server Name, its Netbios Name.
'===================================================================

Set oRelationshipInstance = g_oDiscData.CreateRelationshipInstance("$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_SMS_Server_Connector_Relationship']$")

oRelationshipInstance.Source = oSiteServerInstance
oRelationshipInstance.Target = oSiteSystemInstance

oRelationshipInstance.AddProperty "$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_SMS_Server_Connector_Relationship']/ArrowStyle$", SITE_SERVER_TO_SITE_SYSTEM_ARROW_STYLE
oRelationshipInstance.AddProperty "$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_SMS_Server_Connector_Relationship']/Connector_Name$", strSiteServerFQDomainName &amp; " - " &amp; strSiteSystemFQDomainName
oRelationshipInstance.AddProperty "$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_SMS_Server_Connector_Relationship']/Connector_Type$", SITE_SERVER_TO_SITE_SYSTEM_CONNECTOR_TYPE
oRelationshipInstance.AddProperty "$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_SMS_Server_Connector_Relationship']/LineColor$", SITE_SERVER_TO_SITE_SYSTEM_LINE_COLOR
oRelationshipInstance.AddProperty "$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_SMS_Server_Connector_Relationship']/LineStyle$", SITE_SERVER_TO_SITE_SYSTEM_LINE_STYLE
oRelationshipInstance.AddProperty "$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_SMS_Server_Connector_Relationship']/LineWeight$", SITE_SERVER_TO_SITE_SYSTEM_LINE_WEIGHT

g_oDiscData.AddInstance oRelationshipInstance

End Sub


'******************************************************************************
' Name: AddSiteServerToSiteSystemInstances
'
' Purpose: Creates and adds the site server to site system connectors,
' relationships, to the discovery data object.
'
' Parameters: oService The Wbem Services object for the
' local provider's site.
' strSiteCode The site code an SMS site.
' strSiteServerName The Netbios name of an SMS site
' server.
' strSiteServerFQDomainName The fully qualified domain name of
' an SMS site server.
'
' Returns: Nothing
'
Sub AddSiteServerToSiteSystemInstances(oService, _
strSiteCode, _
strSiteServerName, _
strSiteServerFQDomainName)

Dim oSiteServerInstance
Dim oSiteSystemSummarizer
Dim oSiteSystemSummarizerSet

Dim strError
Dim strQuery
Dim strSiteSystemName
Dim strSiteSystemFQDomainName


On Error Resume Next


'Check input parameters.
'=======================

If IsEmpty(oService) Or _
IsEmpty(strSiteCode) Or _
IsEmpty(strSiteServerName) Or _
IsEmpty(strSiteServerFQDomainName) Then
Err.Raise E_INVALIDARG
LogScriptError "Invalid parameter passed to AddSiteServerToSiteSystemInstances."
Exit Sub
End If

LogMessage DBG_TRACE, "Discovering connections site server " &amp; strSiteServerFQDomainName &amp; _
" has to its site systems."

'Create and add an SMS_Server class instance for the specified site
'server. This will not be a complete discovery object as server
'discovery will do this since it runs locally on the computer.
'==================================================================

Set oSiteServerInstance = g_oDiscData.CreateClassInstance("$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_Class']$")
oSiteServerInstance.AddProperty "$MPElement[Name='SystemLibrary!System.Entity']/DisplayName$", DISPLAY_NAME_SMS_SERVER &amp; " - " &amp; strSiteServerName
oSiteServerInstance.AddProperty "$MPElement[Name='Microsoft.SystemCenter.ConfigurationManager.2007.SMS_Server_Class']/Name_1$", strSiteServerName
oSiteServerInstance.AddProperty "$MPElement[Name='WindowsLibrary!Microsoft.Windows.Computer']/PrincipalName$", strSiteServerFQDomainName
g_oDiscData.AddInstance oSiteServerInstance

'Query for only site systems in the specified site.
'==================================================

strQuery = GetSMSSSiteSystemSummarizerQuery(strSiteCode, strSiteServerName)

Set oSiteSystemSummarizerSet = oService.ExecQuery(strQuery)

If Not IsEmpty(oSiteSystemSummarizerSet) Then

For Each oSiteSystemSummarizer In oSiteSystemSummarizerSet

If Not IsEmpty(oSiteSystemSummarizer) Then

strSiteSystemName = oSiteSystemSummarizer.ServerName

strSiteSystemFQDomainName = GetFQDomainName(oService, _
strSiteCode, _
strSiteSystemName)

AddSiteServerToSiteSystemInstance oSiteServerInstance, _
strSiteServerFQDomainName, _
strSiteSystemName, _
strSiteSystemFQDomainName

If Err Then
LogScriptError "Failed to add Site Server to Site System Instance."
Exit For
End If

End If

Next

Set oSiteSystemSummarizerSet = Nothing

End If

End Sub


'******************************************************************************
' Name: GetErrorString
'
' Purpose: Attempts to find the description for an error if an error with
' no description is passed in.
'
' Parameters: oErr The error object
'
' Return: String, the description for the error. (Includes the error code.)
'
Function GetErrorString(oErr)

Dim lErr, strErr


lErr = oErr
strErr = oErr.Description

On Error Resume Next

If 0 &gt;= Len(strErr) Then

' If we don't have an error description, then check to see if the error
' is a 0x8007xxxx error. If it is, then look it up.
Const ErrorMask = &amp;HFFFF0000
Const HiWord8007 = &amp;H80070000
Const LoWordMask = 65535 ' This is equivalent to 0x0000FFFF

If (lErr And ErrorMask) = HiWord8007 Then

' Attempt to use 'net helpmsg' to get a description for the error.
Dim oShell
Set oShell = CreateObject("WScript.Shell")

If Err = 0 Then

Dim oExec
Set oExec = oShell.Exec("net helpmsg " &amp; (lErr And LoWordMask))

Dim strMessage, i
Do
strMessage = oExec.stdout.ReadLine()
i = i + 1
Loop While (Len(strMessage) = 0) And (i &lt; 5)

strErr = strMessage

End If

End If

End If

GetErrorString = vbCrLf &amp; "The error returned was: '" &amp; strErr &amp; "' " &amp; lErr &amp; " (0x" &amp; Hex(lErr) &amp; ")"

End Function


'******************************************************************************
' Name: ScriptError
'
' Purpose: To generate a warning about a runtime script error.
'
' Parameters: strError The description of the error
'
' Returns: Nothing
'
Sub ScriptError(strError)

LogEvent EVENT_ID_SCRIPTERROR, EVENT_TYPE_WARNING, "encountered a runtime error." &amp; vbCrLf &amp; strError

End Sub


'******************************************************************************
' Name: AccessError
'
' Purpose: To generate a warning about an access/permissions error.
'
' Parameters: strError The description of the error
' strNameSpace The WMI name space root
'
' Returns: Nothing
'
Sub AccessError(strError, strNameSpace)

LogEvent EVENT_ID_ACCESSDENIED, EVENT_TYPE_WARNING, "encountered a permissions error." &amp; vbCrLf &amp; _
"The script failed to " &amp; strError &amp; " in the naming context '" &amp; _
strNameSpace &amp; "' because access was denied. Alter the permissions for this " &amp; _
"name space so that the script." &amp; vbCrLf &amp; _
GetErrorString(Err)
End Sub


'******************************************************************************
' Name: LogEvent
'
' Purpose: To generate a MOM event
'
' Parameters: lEventID The event code
' lEventType The severity of the event
' strMessage The message to include in the event
'
' Returns: Nothing
'
Sub LogEvent(lEventID, lEventType, strMessage)

g_oMomScriptAPI.LogScriptEvent SCRIPT_NAME, lEventID, lEventType, "The script '" &amp; SCRIPT_NAME &amp; "' " &amp; strMessage

End Sub


'******************************************************************************
' Name: LogMessage
'
' Purpose: To log a message to either a message box or the command console
' window.
'
' Parameters: lLevel The debug level for this message i.e. trace,
' warning or error
' strMessage The message to write
'
' Returns: Nothing
'
Sub LogMessage(lLevel, strMessage)

If (lLevel &lt; DBG_NONE) Then

If (lLevel = DBG_ERROR) Then
WScript.Echo "[Error]: " + strMessage
ElseIf (lLevel = DBG_WARNING) Then
WScript.Echo "[Warning]: " + strMessage
ElseIf (lLevel = DBG_TRACE) Then
WScript.Echo "[Trace]: " + strMessage
End If

End If

End Sub


'******************************************************************************
' Name: LogAndReportScriptError
'
' Purpose: To log a message to either a message box or the command console
' window and create a script error event. The error reported is
' based on the current Err properties.
'
' Parameters: strMessage The message to write.
'
' Returns: Nothing
'
Sub LogScriptError(strMessage)

Dim lError

Dim strError


lError = Err.number
strError = GetErrorString(Err)

LogMessage DBG_ERROR, strMessage &amp; strError

ScriptError strMessage &amp; strError

Err.Raise lError

End Sub


'******************************************************************************
' Name: IsEventTracingEnabled
'
' Purpose: Returns the state of the EventTracingEnabled registry setting.
' This setting is directs scripts raise additional tracing MOM
' events i.e. start and completion events.
'
' Checks for the EventTracingEnabled registry value under the
' following registry paths. If the DWORD values exists, it is read.
'
' HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft Operations Manager
' HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft Operations Manager
'
' Parameters: None
'
' Returns: True if the value exists and is set to 1 else False.
'
Function IsEventTracingEnabled()

Dim strError
Dim lEventTracingEnabled

Dim objShell


On Error Resume Next


LogMessage DBG_TRACE, "Checking EventTracingEnabled registry value."

'Create the WSH Shell object for accessing the registry.
'=======================================================

Set objShell = CreateObject("WScript.Shell")

If Err.number &lt;&gt; 0 Then
IsEventTracingEnabled = False
Exit Function
End If

'Read the EventTracingEnabled value from under the MOM key.
'==========================================================

lEventTracingEnabled = objShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft Operations Manager\EventTracingEnabled")

If IsEmpty(lEventTracingEnabled) Then

lEventTracingEnabled = objShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft Operations Manager\EventTracingEnabled")

End If

'Return the appropriate value.
'=============================

If IsEmpty(lEventTracingEnabled) Then

strError = GetErrorString(Err)
LogMessage DBG_TRACE, "Failed to read EventTracingEnabled registry value." &amp; strError

IsEventTracingEnabled = False

Else

LogMessage DBG_TRACE, "EventTracingEnabled registry value is '" &amp; lEventTracingEnabled &amp; "'."

If (lEventTracingEnabled = 1) Then
IsEventTracingEnabled = True
Else
IsEventTracingEnabled = False
End If

End If

Set objShell = Nothing

Err.Clear

End Function</Script></Contents>
</File>
</Files>
</DataSource>
</Discovery>