Link to home
Start Free TrialLog in
Avatar of Jeffrey Renfroe
Jeffrey RenfroeFlag for United States of America

asked on

Better way to add a machine to multiple collections

I received a script to add a single machine to a single collection. I have modified it to allow me to add one machine to two collections. I am trying to find a better way to do this. Is there a way to do this without adding/changing the below?

Set colResourceIDs=objSMS.ExecQuery _
    ("SELECT ResourceId FROM SMS_R_System WHERE NetbiosName ='" & _
         strComputerName & "'")
For Each insResource In colResourceIDs
        strNewResourceID = insResource.ResourceID
Next
'add the ResourceID to the collection
Set instColl = objSMS.Get _
    ("SMS_Collection.CollectionID=""" & strCollID1 & """")
Set instDirectRule = objSMS.Get _
    ("SMS_CollectionRuleDirect").SpawnInstance_ ()
instDirectRule.ResourceClassName = "SMS_R_System"
instDirectRule.ResourceID = strNewResourceID
instDirectRule.RuleName = strComputerName
instColl.AddMembershipRule instDirectRule
strSMSServer = "TLNASMS01"
 
strCollID0 = "PN000164" 'ID of the collection
strCollID1 = "PN000104"
strComputerName = "TLNAW00016" 'computer name to add
 
Set objLoc =  CreateObject("WbemScripting.SWbemLocator")
Set objSMS= objLoc.ConnectServer(strSMSServer, "root\sms")
Set Results = objSMS.ExecQuery _
    ("SELECT * From SMS_ProviderLocation WHERE ProviderForLocalSite = true")
For each Loc in Results
    If Loc.ProviderForLocalSite = True Then
        Set objSMS = objLoc.ConnectServer(Loc.Machine, "root\sms\site_" & _
            Loc.SiteCode)
    end If
Next
 
'obtain the ResourceID for strComputerName
Set colResourceIDs=objSMS.ExecQuery _
    ("SELECT ResourceId FROM SMS_R_System WHERE NetbiosName ='" & _
         strComputerName & "'")
For Each insResource In colResourceIDs
        strNewResourceID = insResource.ResourceID
Next
'add the ResourceID to the collection
Set instColl = objSMS.Get _
    ("SMS_Collection.CollectionID=""" & strCollID0 & """")
Set instDirectRule = objSMS.Get _
    ("SMS_CollectionRuleDirect").SpawnInstance_ ()
instDirectRule.ResourceClassName = "SMS_R_System"
instDirectRule.ResourceID = strNewResourceID
instDirectRule.RuleName = strComputerName
instColl.AddMembershipRule instDirectRule
 
Set colResourceIDs=objSMS.ExecQuery _
    ("SELECT ResourceId FROM SMS_R_System WHERE NetbiosName ='" & _
         strComputerName & "'")
For Each insResource In colResourceIDs
        strNewResourceID = insResource.ResourceID
Next
'add the ResourceID to the collection
Set instColl = objSMS.Get _
    ("SMS_Collection.CollectionID=""" & strCollID1 & """")
Set instDirectRule = objSMS.Get _
    ("SMS_CollectionRuleDirect").SpawnInstance_ ()
instDirectRule.ResourceClassName = "SMS_R_System"
instDirectRule.ResourceID = strNewResourceID
instDirectRule.RuleName = strComputerName
instColl.AddMembershipRule instDirectRule

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of merowinger
merowinger
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jeffrey Renfroe

ASKER

This is great. I actually understand an array better. Thank you
This is great. Thank you