Populating groups from a text file - step by step

Published Jan 21 2019 04:37 PM 129 Views

First published on MSDN on Mar 15, 2010

Kevin recently posted a blog entry on how to use a SQL database to determine and populate group membership – his post is available here .   But what if you have your data in some other location – such as a text file?  How can you use that text file to do the same thing?  It’s actually very easy – let’s walk through how to set it up.  I include the complete MP and sample input file at the bottom of this post for reference.


In my lab I have configured a simple comma delimited text file for input that describes systems in my environment and their location.  For my test I will call this file extract.txt since it’s contents were extracted from a database.


Sample Input File


From this data I want to build groups of all systems in the Central, West and Southeast regions.  To do this I just have to build out some simple XML.  I’ll use Kevin’s same format for showing the XML


Section 1: <Manifest>

Simply modify the <ID>, <Version>, and <Name> sections based on your custom MP naming standard.  Note also the first line is a definition line and not part of the manifest section but included here and required for the MP.


<ManagementPack ContentReadable="true" xmlns:xsd=" http://www.w3.org/2001/XMLSchema" xmlns:xsl=" http://www.w3.org/1999/XSL/Transform" >
<Reference Alias="SC">
<Reference Alias="Windows">
<Reference Alias="Health">
<Reference Alias="System">


Section 2: <TypeDefinitions>


The example below defines 3 groups, and the relationships for those groups (contains windows computer objects).  Simply replace the bolded red “WestGroup” example with a short name for your custom groups.  (We will define the UI friendly name later, in the <Presentation> section.


<ClassType ID="GroupPopulation.WestGroup" Accessibility="Internal" Abstract="false" Base="System!System.Group" Hosted="false" Singleton="true" />
<ClassType ID="GroupPopulation.CentralGroup" Accessibility="Internal" Abstract="false" Base="System!System.Group" Hosted="false" Singleton="true" />
<ClassType ID="GroupPopulation.SoutheastGroup" Accessibility="Internal" Abstract="false" Base="System!System.Group" Hosted="false" Singleton="true" />
<RelationshipType ID="GroupPopulation.WestGroupContainsWindowsComputers" Accessibility="Internal" Abstract="false" Base="System!System.Containment">
<RelationshipType ID="GroupPopulation.CentralGroupContainsWindowsComputers" Accessibility="Internal" Abstract="false" Base="System!System.Containment">
<RelationshipType ID="GroupPopulation.SoutheastGroupContainsWindowsComputers" Accessibility="Internal" Abstract="false" Base="System!System.Containment">


Section 3: <Monitoring>


In this section – we define the discovery, and add the script to run.  In this example – I am running a VBscript to loop through our text file looking for matches on region.  You will need to modify the group name – just like you did above.  This is where we create a discovery to populate each group – so we will need one of these sections for each group in the MP.  Each of these sections will run a distinct script, with a different criteria, depending on which region you want populated.


I bolded in RED the group name sections you will need to modify, just like we did above… and you will need to modify the criteria information, and the script name.


I set the discovery time to every 3600 seconds in this example…. you should probably set this to once or twice a day max…. no need to keep re-running it for groups that wont change that often.


<Discovery ID="WestGroup.Discovery" Enabled="true" Target="SC!Microsoft.SystemCenter.RootManagementServer" ConfirmDelivery="true" Remotable="true" Priority="Normal">
<DiscoveryClass TypeID="GroupPopulation.WestGroup" />
<DataSource ID="DS" TypeID="Windows!Microsoft.Windows.TimedScript.DiscoveryProvider">
<SyncTime />
<Arguments>$MPElement$ $Target/Id$</Arguments>
<ScriptBody><![CDATA[Dim SourceId
Dim objConnection
Dim oRS
Dim sConnectString
Dim ManagedEntityID
Dim oAPI
Dim oDiscoveryData
Dim oAttributes
Dim oLocation
Dim oHostName
Dim oMOdel
Dim oState
Dim oRegion
Dim oFSO
Dim oINst
dim fsoFile
SourceId                     = WScript.Arguments(0)
ManagedEntityId        = WScript.Arguments(1)


Set oAPI                      = CreateObject("MOM.ScriptAPI")
Set oDiscoveryData     = oAPI.CreateDiscoveryData(0,SourceId,ManagedEntityId)
Set oFSO                     = CreateObject("Scripting.FileSystemObject")
Set groupInstance      = oDiscoveryData.CreateClassInstance("$MPElement[Name='GroupPopulation.WestGroup']$")
set fsoFile                   = ofso.opentextfile("c:\extract.txt", 1)




Do Until fsofile.AtEndOFStream


If oRegion="West" Then
Set ServeriNstance = oDiscoveryData.CreateClassInstance("$MPElement[Name='Windows!Microsoft.Windows.Computer']$")
serverInstance.AddProperty "$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$",oHostName
Set relationshipInstance = oDiscoveryData.CreateRelationshipInstance("$MPElement[Name='GroupPopulation.WestGroupContainsWindowsComputers']$")
relationshipInstance.Source = groupInstance
relationshipInstance.Target = serverInstance
oDiscoveryData.AddInstance relationshipInstance
End If


'Once the discovery data is obtained it needs to be submitted
Call oAPI.Return(oDiscoveryData)




Section 4: <LanguagePacks>

<LanguagePack ID="ENU" IsDefault="true">
<DisplayString ElementID="RegionBasedGroupDemo">
<Name>Region Based Group Population Demo MP</Name>


<DisplayString ElementID="GroupPopulation.WestGroup">
<Name>West Servers Group</Name>
<DisplayString ElementID="GroupPopulation.WestGroupContainsWindowsComputers">
<Name>West Based Group Contains Windows Computers</Name>
<DisplayString ElementID="WestGroup.Discovery">
<Name>West Based Group Discovery</Name>
<Description />


<DisplayString ElementID="GroupPopulation.CentralGroup">
<Name>Central Servers Group</Name>
<DisplayString ElementID="GroupPopulation.CentralGroupContainsWindowsComputers">
<Name>Central Based Group Contains Windows Computers</Name>
<DisplayString ElementID="CentralGroup.Discovery">
<Name>Central Based Group Discovery</Name>
<Description />


<DisplayString ElementID="GroupPopulation.SoutheastGroup">
<Name>Southeast Servers Group</Name>
<DisplayString ElementID="GroupPopulation.SoutheastGroupContainsWindowsComputers">
<Name>Southeast Based Group Contains Windows Computers</Name>
<DisplayString ElementID="SoutheastGroup.Discovery">
<Name>Southeast Based Group Discovery</Name>
<Description />




That’s it!  If you get errors trying to import – you most likely modified a definition incompletely…. the import error should help you figure out what's wrong.


Now I can go to my groups – find “West Group” and see if it is populated:  SUCCESS!



TextFileBasedGroups-Sample MP and Data File.zip

Version history
Last update:
‎Apr 07 2020 10:12 AM
Updated by: