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

Comments

  • How would you form your Select if you would want the search to be done in a specific collection ?
  • 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?
Please log in to comment

Community Chosen Answer

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")
Answered 10/23/2012 by: captain_planet
Third Degree Brown Belt

  • Thanks !! You guys are great !
  • NIce script,can some one help me with a script that can remove machine from collection

    Thanks
  • 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
Please log in to comment

Answers

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_

 

Answered 10/23/2012 by: mahendraKumar
Senior Yellow Belt

Please log in to comment
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??
Answered 02/16/2015 by: jhedelin
White Belt

Please log in to comment
Answer this question or Comment on this question for clarity
Admin Script Editor
Admin Script Editor is an integrated scripting environment available free here at ITNinja

Share