Skip to content

Possible to use script to add computers to a collection?

Featured Replies

All,

 

Is there a possibility to use a script to add computernames to a collection?

If so, has anybody an example of such a script?

 

Reasoning behind it is that software deployment with AD groups is not fast enough, and we don't want all helpdesk persons mocking around in the console itself.

You could use something like this:

 
Option Explicit ' --- Force Declaration of variables
DIM objSWbemLocator, objSWbemServices, ProviderLocation, Location
DIM sCollectionName, sComputername, colComputerItemSet, objComputerItem, FSO, logFile
DIM colCollectionItemSet, objCollectionItem, objCollectionRule, objArguments, sResourceID, sRuleName
'===============================================================================
' Check arguments
'===============================================================================
Set objArguments = Wscript.Arguments
If WScript.Arguments.Count = 2 Then
  sCollectionName = objArguments.Named.Item("CollectionName")
  sComputername = objArguments.Named.Item("ComputerName")
Else
  Wscript.Echo "Usage: AddComputerToCollection.vbs /CollectionName:[CollectionName] /ComputerName:[ComputerName]"
  Wscript.Quit
End If
'===============================================================================
' MAIN Script to ADD a Computer to a Collection
'===============================================================================
ConnectToSMSProvider()
Set FSO = CreateObject ("Scripting.FileSystemObject")
Set logFile = FSO.CreateTextFile ("AddComputerToCollectionLOG.csv")
Set colComputerItemSet = objSWbemServices.ExecQuery("Select * From SMS_R_System WHERE Name LIKE '" & sComputername & "'")
For Each objComputerItem In colComputerItemSet
  If UCase(objComputerItem.Name) = UCase(sComputerName) Then
 	sResourceID = objComputerItem.ResourceID
 	sRuleName = objComputerItem.NetBiosName
  End If
Next
Set colCollectionItemSet = objSWbemServices.ExecQuery("Select * From SMS_Collection")
For Each objCollectionItem In colCollectionItemSet
  If objCollectionItem.Name = sCollectionName Then
 	Set objCollectionRule = objSWbemServices.Get("SMS_CollectionRuleDirect").SpawnInstance_()
  objCollectionRule.ResourceClassName = "SMS_R_System"
  objCollectionRule.RuleName = sRuleName
  objCollectionRule.ResourceID = sResourceID
  objCollectionItem.AddMembershipRule objCollectionRule
  logFile.Write "Succesfully added " & sComputerName & " to collection: " & sCollectionName
  End If
Next
'===============================================================================
' Sub Routine to Connect to the SMS Provider
'===============================================================================
Sub ConnectToSMSProvider()
  Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
  Set objSWbemServices = objSWbemLocator.ConnectServer("PTSRVR02", "root\sms")
  Set ProviderLocation = objSWbemServices.InstancesOf("SMS_ProviderLocation")
  For Each Location In ProviderLocation
 	If Location.ProviderForLocalSite = True Then
    	Set objSWbemServices = objSWbemLocator.ConnectServer(Location.Machine, "root\sms\site_" + Location.SiteCode)
 	End If
  Next
End Sub
WScript.Quit(0)

 

But AFAIK there are some tools (and webservices) on the web that can provide something like this with a nice interface :)

How often is your SCCM server scanning AD? You can tinker with those settings to deploy software a little more quickly.

 

One of the gripes I have about SCCM is the inability to easily push software immediately to a system.

  • Author

Peter,

 

Do you have any such examples of tools?

I have found the Sccm Client Injector on SourceForge, but we would need to add an email feature to it and the source files are missing.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.