Shaun Cassells at MyITForum.com

Systems Management Server (SMS) 2003, System Center Configuration Manager (SCCM or ConfigMan) 2007, PowerShell, scripting and security (including patching), Finance, Fitness and Fun

August 2007 - Posts

SMS 2003 Script to Delete Collections based on Criteria
The following VBS script is used to delete collections.

Script Required Inputs

  1. SMS Server Name
  2. SMS Site Code (of the server)
  3. Collection Search String
  4. Optional Commit of a capital “Y”
    1. The 4th parameter is a commit of collection deletion.  This means you can run the script to see all the collections that match your search string BEFORE you delete.

Example Usage

  • Cscript DelCollection.vbs SMSServer XYZ “Microsoft Visio 2007 - %”

·        Used to display all collections matching string

  • Cscript DelCollection.vbs SMSServer XYZ Visio_2007 Y

·        Used to delete all collections matching string

  Shaun’s helpful note on search strings for SQL / WQL.

Wild cards in SQL

  • %
    • match any number of characters (letters or numbers or symbols)
    • “Micr%ft”
      • will match any combination of characters between “Micr” and “ft”
  • \
    • Is the escape character.  You can search for the “%” character in a string
    • “Micr\%ft”
      • will match the exact string of “Micr%ft”
  • “”
    • Double quotes are used to match string with spaces
    • “Search for this”
      • will match the exact string of “Search for this”
  • []
    • matches exact characters or ranges
    • [a,d,g]
      • will match any of those 3 characters at that specific location
    • [g-t]
      • will match any letter between g and t (g, h, i, j, etc)
    • [1,3,9]
      • will match any of those 3 numbers at that specific location
  • Search wild cards can be used in combination
    • Assuming you collections with 10 digit date.
      • Adobe – Acrobat - 03272007
    • To find all collections with the date range of January – June and August excluding July of 2007.  You also want to be as specific as possible so as not to match anything else
      • Search string
        • “% - 0[1-6,8][0-9][0-9]2007”

DelCollections.VBS

  ' Delete Collections from SMS Server
' select Name from SMS_Collection where name like 3rd input
' Created by Shaun Cassells
Dim loc
Dim oArgs
Dim strComputer, ResID
Dim strServer, strSiteCode
Set oArgs = WScript.ArgumentsIf oArgs.Count = 3 Then WSCRIPT.ECHO "---------------------------------------------------------------"
 WSCRIPT.ECHO "Please use 3/4 inputs"
 WSCRIPT.ECHO " SMS Server"
 WSCRIPT.ECHO " SMS Site Code"
 WSCRIPT.ECHO " Collection Name Query to match on"
 WSCRIPT.ECHO " Blank displays results / Y = delete"
 WSCRIPT.ECHO "---------------------------------------------------------------"
 WSCRIPT.ECHO ""
End If
If (oArgs.Count < 2) or (oArgs.Count > 4) Then 
 WSCRIPT.ECHO "---------------------------------------------------------------"
 WSCRIPT.ECHO "Please use 3/4 inputs"
 WSCRIPT.ECHO " SMS Server"
 WSCRIPT.ECHO " SMS Site Code"
 WSCRIPT.ECHO " Collection Name Query to match on"
 WSCRIPT.ECHO " Blank displays results / Y = delete"
 WSCRIPT.ECHO "---------------------------------------------------------------"
 WSCRIPT.ECHO ""
 WSCRIPT.QUIT
End If
   strServer = oArgs(0)
   strSiteCode = oArgs(1)
   strCollection = oArgs(2)
   If oArgs.Count = 4 Then
    strQuery = oArgs(3)
   End If
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Set loc = CreateObject( "WbemScripting.SWbemLocator" )
Set WbemServices = loc.ConnectServer( strServer,"root\SMS\site_" & strSiteCode)
'WScript.Echo strComputer & " ResourceID in " & strServer & " is " & ResID
strQry =  "select * from SMS_Collection where name like '%" & strCollection & "%'"
Set instances = WbemServices.ExecQuery(strQry)


If instances.Count = 0 Then
 Else
 For Each instance In instances
 WScript.Echo instance.name
 If strQuery = "Y" Then
  instance.Delete_
 End If
 Next
 End If


Set instances = NOTHING
Set WbemServices = NOTHING
Set strQry = NOTHING
Set loc = NOTHING