Solved

Better way to add a machine to multiple collections

Posted on 2009-04-13
3
528 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Have you considered what group policies are backwards and forwards compatible? Windows Active Directory servers and clients use group policy templates to deploy sets of policies within your domain. But, there is a catch to deploying policies. The…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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