Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Better way to add a machine to multiple collections

Posted on 2009-04-13
3
Medium Priority
?
540 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 31

Accepted Solution

by:
merowinger earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Welcome to my series of short tips on migrations. Whilst based on Microsoft migrations the same principles can be applied to any type of migration. My first tip is around source server preparation. No migration is an easy migration, there is a…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

610 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