/build/static/layout/Breadcrumb_cap_w.png

ConfigMgr Collection VBscript

Can someone help me with this, I'm looking for a script that will add a computer (prompt for computer name) to a pre-define SCCM Collection ?  Or a script that will delete a computer from the All Systems Collection. 


2 Comments   [ + ] Show comments
  • How would you form your Select if you would want the search to be done in a specific collection ? - Lemelin1 10 years ago
  • If i run this vb script locally on client it works fine, but when i create a package in SCCM it does not work when running with SYSTEM account. Any solution for this? - jhedelin 9 years ago

Answers (3)

Posted by: captain_planet 11 years ago
Black Belt
1

This script will add a machine name to a collection (once you configure the constants at the top).  It isn't my script (I won't take the credit...) but it works well from memory:

 '########## PARAMETERS ###############
 
'Define the target collection using the CollectionID value
Const CollectionId = "CEN003A5"
 
'Define the SCCM Site Server
Const SiteServer = "TAL001"
Const SiteCode = "TAL"
 
'########## PROGRAM ###############
 
Dim sResourceID, oSMS
Dim wShell : Set wShell = WScript.CreateObject( "WScript.Shell" )
 
'Use the local computer value as the target machine
Dim sComputer : sComputer = wShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
 
'Main Program Runs Here…
wScript.echo sComputer
Call ConnectToSite() : 'Connect to Primary Site Server
Call AddMachineToCollection() 'Add machine to specified collection
 
'########## FUNCTIONS ###############
 
'Query site for machine and return resource ID
Function FindMachine
 
    'Query WMI for the machine
    Set oResults = oSMS.ExecQuery("SELECT * FROM SMS_R_System WHERE Name = '" & sComputer & "'")
 
    For Each oResourceID In oResults
        FindMachine = oResourceID.ResourceID 'Return the result of the query
    Next
End Function
 
'Connect to the SCCM server
Sub ConnectToSite()
 
    On Error Resume Next
    Set oLocator = CreateObject("WbemScripting.SWbemLocator")
 
    'Connect to the SCCM Site Server
    Set oSMS = oLocator.ConnectServer(SiteServer, "root\sms\site_" & SiteCode)
 
    'Quit the script with an error message if unable to connect
    If Err Then
        Err.Clear
        wScript.echo "Cannot connect to SCCM."
        wScript.Quit
    End If
 
    wScript.echo "Connected to " & SiteCode
    oSMS.Security_.ImpersonationLevel = 3
    oSMS.Security_.AuthenticationLevel = 6
End Sub
 
'Create a membership rule to add the machine to the collection
Sub AddMachineToCollection()
 
    'Return the resourceID of the machine
    sResourceID = FindMachine
 
    'Set the machine collection
    Dim oCollection : Set oCollection = oSMS.Get("SMS_Collection.CollectionID=" & """" & CollectionId & """")
    'Create a membership rule
    Dim oCollectionRule : Set oCollectionRule = oSMS.Get("SMS_CollectionRuleDirect").SpawnInstance_()
 
    'Define the membership rules properties
    oCollectionRule.ResourceClassName = "SMS_R_System"
    oCollectionRule.ResourceID = sResourceID
    oCollection.AddMembershipRule oCollectionRule : 'Create membership rule
 
    wScript.echo "Added to collection."
End Sub

although based on your question, you'll probably want to replace this line:

 Dim sComputer : sComputer = wShell.ExpandEnvironmentStrings("%COMPUTERNAME%")

with this:

 Dim sComputer : sComputer = InputBox("Please Enter your Machine Name:","Please Enter your Machine Name")

Comments:
  • Thanks !! You guys are great ! - Lemelin1 11 years ago
  • NIce script,can some one help me with a script that can remove machine from collection

    Thanks - Afzalali 10 years ago
  • and also can some tell how to make changes to above script so that it can promt for collection name as well like how it asked for machine details - Afzalali 10 years ago
Posted by: mahendraKumar 11 years ago
Senior Yellow Belt
0

you can just run a query to find out the collection id and you can add or delete .. i could not find my complete script... but remeber below object... will try to get it posted soon.

 

    Set col = objSMS.Get _

    ("SMS_Collection.CollectionID='" & strCollID & "'")

Col.Delete_

 

Posted by: jhedelin 9 years ago
White Belt
0
When i run this script locally on client computer it works perfect.
But when i try to run it as a package from SCCM its failing.

Anyone with this problem??
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ