Solved

Better way to add a machine to multiple collections

Posted on 2009-04-13
3
524 Views
Last Modified: 2013-11-21
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

0
Comment
Question by:Lorrec
  • 2
3 Comments
 
LVL 31

Accepted Solution

by:
merowinger earned 500 total points
Comment Utility
should be possible with an array an a for each loop.
Have a  look below!
strSMSServer = "TLNASMS01"

arrCollID = Array("PN000164","PN000165","PN000164") 'ID of the collection

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
 

'Loop through all collection ids

For Each currentColl In arrCollID

	'add the ResourceID to the collection

	Set instColl = objSMS.Get ("SMS_Collection.CollectionID=""" & currentColl & """")

	Set instDirectRule = objSMS.Get ("SMS_CollectionRuleDirect").SpawnInstance_ ()

	instDirectRule.ResourceClassName = "SMS_R_System"

	instDirectRule.ResourceID = strNewResourceID

	instDirectRule.RuleName = strComputerName

	instColl.AddMembershipRule instDirectRule

Next

Open in new window

0
 

Author Comment

by:Lorrec
Comment Utility
This is great. I actually understand an array better. Thank you
0
 

Author Closing Comment

by:Lorrec
Comment Utility
This is great. Thank you
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Some time ago I faced the need to use a uniform folder structure that spanned across numerous sites of an enterprise to be used as a common repository for the Software packages of the Configuration Manager 2007 infrastructure. Because the procedu…
Remote Apps is a feature in server 2008 which allows users to run applications off Remote Desktop Servers without having to log into them to run the applications.  The user can either have a desktop shortcut installed or go through the web portal to…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now