Jump to content


  • 0
bvanbeekadm

Possible to use script to add computers to a collection?

Question

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.

Share this post


Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0

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 :)

Share this post


Link to post
Share on other sites

Join the conversation

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

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.