Solved

Better way to add a machine to multiple collections

Posted on 2009-04-13
3
527 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
ID: 24150065
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
ID: 24150160
This is great. I actually understand an array better. Thank you
0
 

Author Closing Comment

by:Lorrec
ID: 31569656
This is great. Thank you
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is my 3rd article on SCCM in recent weeks, the 1st (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Server/Windows_Server_2008/A_4466-A-beginners-guide-to-installing-SCCM2007-on-Windows-2008-R2-Server.html) dealing with installat…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

919 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

21 Experts available now in Live!

Get 1:1 Help Now