Function GetStdRegProvObject
Dim strComputer
strComputer = "."
Set GetStdRegProvObject = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
End Function
Function GetStringValue(strKeyPath, strValueName)
Const HKEY_LOCAL_MACHINE = &H80000002
Dim objRegistry
Set objRegistry = GetStdRegProvObject
objRegistry.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, GetStringValue
Set objRegistry = Nothing
End Function
Function GetDWORDValue(strKeyPath, strValueName)
Const HKEY_LOCAL_MACHINE = &H80000002
Dim objRegistry
Set objRegistry = GetStdRegProvObject
objRegistry.GetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, GetDWORDValue
Set objRegistry = Nothing
End Function
Function GetValues(strKeyPath)
Const HKEY_LOCAL_MACHINE = &H80000002
Dim objRegistry
Set objRegistry = GetStdRegProvObject
objRegistry.EnumValues HKEY_LOCAL_MACHINE, strKeyPath, GetValues, Nothing
Set objRegistry = Nothing
End Function
Function StringValueExists(strKeyPath, strValueName)
StringValueExists = Not IsNull(GetStringValue(strKeyPath, strValueName))
End Function
Function ValuesExist(strKeyPath)
ValuesExist = Not IsNull(GetValues(strKeyPath))
End Function
Function GetMultipleServerGroup(strKeyPath)
Dim strValueNames, strValueName, strValue, strServerGroup
strValueNames = GetValues(strKeyPath)
For Each strValueName in strValueNames
If IsNumeric(strValueName) Then
strValue = GetStringValue(strKeyPath, strValueName)
If strValueName = strValueNames(0) Then
strServerGroup = strValue
Else
strServerGroup = strServerGroup & ", " & strValue
End If
End If
Next
GetMultipleServerGroup = strServerGroup
End Function
Dim oArgs
Set oArgs = WScript.Arguments
If oArgs.Count < 4 Then
Wscript.Quit -1
End If
Dim SourceID, ManagedEntityId, TargetComputer, HostedCache, SingleHostedCache, GPHostedCache, GPHostedCacheExists
If GPHostedCacheExists = "False" Then
'Windows 8 Hosted Cache (Group Policy)
If ValuesExist(strGPMultipleServersKey) Then
HostedCache = GetMultipleServerGroup(strGPMultipleServersKey)
GPHostedCacheExists = "True"
GPHostedCache = HostedCache
'Windows 8 Hosted Cache (Local)
ElseIf ValuesExist(strMultipleServersKey) Then
HostedCache = GetMultipleServerGroup(strMultipleServersKey)
'Auto-Discovery (SCP) Hosted Cache (Group Policy)
ElseIf GetDWORDValue(strGPDiscoveryKey, "SCPDiscoveryEnabled") >= 1 And ValuesExist(strDiscoveredServersKey) Then
HostedCache = GetMultipleServerGroup(strDiscoveredServersKey)
GPHostedCacheExists = "True"
GPHostedCache = HostedCache
'Auto-Discovery (SCP) Hosted Cache (Local)
ElseIf GetDWORDValue(strDiscoveryKey, "SCPDiscoveryEnabled") >= 1 And ValuesExist(strDiscoveredServersKey) Then
HostedCache = GetMultipleServerGroup(strDiscoveredServersKey)
'Legacy Hosted Cache
ElseIf StringValueExists(strConnectionKey, "Location") Then
HostedCache = GetStringValue(strConnectionKey, "Location")
SingleHostedCache = HostedCache
Else
WScript.Quit -1
End If
End If
''
'' Declare some working variables and then create an empty discovery payload.
''
Dim oAPI, oDiscoveryData, cInst, mInst, bInst, appInst, relInst, relInst2, relInst3
Set oAPI = CreateObject("MOM.ScriptAPI")
set oDiscoveryData = oAPI.CreateDiscoveryData(0, SourceId, ManagedEntityId)
''
'' Now, let's discover the consensus node and set its properties. When done we add it to the discovery packet
''
set cInst = oDiscoveryData.CreateClassInstance("$MPElement[Name='Microsoft.Windows.BranchCache.PowerShell.HostedCacheClientNode']$")
''
'' Since we are using "contains" - we have to now discover the class instance above us. If it is
'' already discovered, Ops Manager will recognize this as a reference count increase and not a
'' separate discovery because the key property is the same.
''
set mInst = oDiscoveryData.CreateClassInstance("$MPElement[Name='Microsoft.Windows.BranchCache.HostedCacheClientGroup']$")
If GPHostedCacheExists = "True" Then
call mInst.AddProperty("$MPElement[Name='Microsoft.Windows.BranchCache.HostedCacheClientGroup']/HostedCacheServerGroupName$", GPHostedCache)
call mInst.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", "HostedCache: "+ GPHostedCache)
Else
call mInst.AddProperty("$MPElement[Name='Microsoft.Windows.BranchCache.HostedCacheClientGroup']/HostedCacheServerGroupName$", HostedCache)
call mInst.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", "HostedCache: "+ HostedCache)
End If
call oDiscoveryData.AddInstance(mInst)
''
'' Now we have to also create an instance of the relationship to link this HostedCacheClientNode to its parent
''
set relInst = oDiscoveryData.CreateRelationshipInstance("$MPElement[Name='Microsoft.Windows.BranchCache.PowerShell.HostedCacheClientGroupContainsHostedCacheClientNode']$")
relInst.Source = mInst
relInst.Target = cInst
call oDiscoveryData.AddInstance(relInst)
''
'' Now we have to also create an instance of the relationship to link this HostedCacheGroup to its parent
''
set relInst2 = oDiscoveryData.CreateRelationshipInstance("$MPElement[Name='Microsoft.Windows.BranchCache.HostedCacheGroupContainsHostedCacheClientGroup']$")
relInst2.Source = bInst
relInst2.Target = mInst
call oDiscoveryData.AddInstance(relInst2)
''
'' BranchCacheAppHostsHostedCacheGroup is using containment
'' We must also discover the BranchCacheApp and create that relationship
''
set appInst = oDiscoveryData.CreateClassInstance("$MPElement[Name='Microsoft.Windows.BranchCache.BranchCacheApp']$")
call appInst.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", "BranchCache")
call oDiscoveryData.AddInstance(appInst)
set relInst3 = oDiscoveryData.CreateRelationshipInstance("$MPElement[Name='Microsoft.Windows.BranchCache.BranchCacheAppHostsHostedCacheGroup']$")
''
'' To force the RMS to re-assign the local agent as the managing agent for the discovered HostedCacheClientNode (cInst)
'' we have to get a reference to the local health service class and then create a SPECIAL SECRET relationship :)
set oHsCnRel = oDiscoveryData.CreateRelationshipInstance("$MPElement[Name='SC!Microsoft.SystemCenter.HealthServiceShouldManageEntity']$")
oHsCnRel.Source = oHealthServiceInstance
oHsCnRel.Target = cInst
call oDiscoveryData.AddInstance(oHsCnRel)
set oHsCnRel2 = oDiscoveryData.CreateRelationshipInstance("$MPElement[Name='SC!Microsoft.SystemCenter.HealthServiceShouldManageEntity']$")
oHsCnRel2.Source = oHealthServiceInstance
oHsCnRel2.Target = mInst
call oDiscoveryData.AddInstance(oHsCnRel2)
''
'' And now we just return the discovery data
''
Call oAPI.Return(oDiscoveryData)</Script></ScriptBody>
<TimeoutSeconds>300</TimeoutSeconds>
</DataSource>
</Discovery>