?
Solved

Editing SCCM Colladd.vbs Script

Posted on 2012-12-21
3
Medium Priority
?
1,440 Views
Last Modified: 2012-12-27
I've been using the colladd.vbs script to add systems to a collection. Im trying to get around using a text file. Intstead i want to be able to input the systems throught the command line with the rest of the arguments (server name, collection ID). If anyone can help me with this it will be much appreciated. I added the code below

***I want to be able to input the system name instead of using the text file***

Set fso = CreateObject("Scripting.FileSystemObject")					 

Set arrArgs = WScript.Arguments
if (arrArgs.Count = 0) Then								'Display Blurb
	wscript.echo ("Colladd.vbs v1.0")					
	wscript.echo ("03/06/03 Mark Nunn")
	wscript.echo ("http://mark.nunn.net")
	wscript.echo ("Colladd.vbs server filename collectionID - to add from file to collection")
	wscript.echo ("Colladd.vbs server - to list collectionID's")
else

	on error resume next								'Some error handling

	strServer=arrArgs(0)								'set variables from command line

	if (arrArgs.Count = 3) Then
		strFile=arrArgs(1)
		strCollID=arrArgs(2)
	end if
	Set objLocator = CreateObject("WbemScripting.SWbemLocator") 			
	Set objSMS = objLocator.ConnectServer(strServer, "Root/SMS")			'connect to sms
	objSMS.Security_.ImpersonationLevel = 3
	wscript.Echo("Connecting to Root/SMS on " & strServer)			
	
	set colSiteDetails=objSMS.ExecQuery("select Machine, SiteCode from SMS_ProviderLocation where ProviderForLocalSite=True")
	For Each insSiteDetails In colSiteDetails
		strSiteCode=insSiteDetails.SiteCode
	next										
	wscript.Echo("Connecting to Root/SMS/site_" & strSiteCode &" on " & strServer)
	set objSMS=objLocator.ConnectServer(strServer, "root/SMS/site_" + strSiteCode)
	wscript.Echo("Connected") 

	if (arrArgs.Count < 3) Then							'if not all arguments supplied list colelctions
		set colCollections=objSMS.ExecQuery("select CollectionID, Name from SMS_Collection ORDER BY CollectionID")
		wscript.echo("CollectionID" & vbTab & "Name")
		For Each insCollection In colCollections
			wscript.echo(insCollection.CollectionID & VbTab & insCollection.Name)
		Next
	else										'otherwise add from file
		set instColl = objSMS.Get("SMS_Collection.CollectionID="&"""" & strCollID & """")
		if Instcoll.Name="" then						'check valid collection
			wscript.echo (strCollId &" Not Found")
		else
			Set filNames = fso.OpenTextFile(strFile)				'open file of machines
			if  (filNames) then
				While not filNames.AtEndOfStream
					strMachine=filNames.ReadLine				'read each line and find resource ID
					set colNewResources=objSMS.ExecQuery("SELECT ResourceId FROM SMS_R_System WHERE NetbiosName ='" & strMachine & "'")	
					strNewResourceID = 0  				
					For each insNewResource in colNewResources
						strNewResourceID = insNewResource.ResourceID
					Next
					if strNewResourceID <> 0 then				'if one exists crate a collection rule
						Set instDirectRule = objSMS.Get("SMS_CollectionRuleDirect").SpawnInstance_ ()
						instDirectRule.ResourceClassName = "SMS_R_System"	
						instDirectRule.ResourceID = strNewResourceID
						instDirectRule.RuleName = "Findres - Replaces " & strResource
						instColl.AddMembershipRule instDirectRule , SMSContext
						instColl.RequestRefresh False
						wscript.echo(strMachine & " Added to " & Instcoll.Name)
					else
						wscript.echo(strMachine & " Not Found")		'otherwise display error
					end if
				WEnd								'next line
			else
				 wscript.echo ("Can't Open " & strfile)				'if file not found
			end if
		end if
	end if
end if

Open in new window

0
Comment
Question by:AnthonySmithMCP
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 2000 total points
ID: 38716955
according to the args, i can see that
args[0] = strServer
args[1] = strFile
args[2] = collection ID

so you want to have a list of system names instead of the 2nd argument, right?

what i would do is switch position of args[1] and args[2]:
args[0] = strServer
args[2] = collection ID
args[3] = list of system names separated by ;

so basically something like:
 cscript /nologo Colladd.vbs smsserver collectionID "system1;system2;system3"

the change in the script is where reading from text file (line 44).

here's the complete script:

Set fso = CreateObject("Scripting.FileSystemObject")					 

Set arrArgs = WScript.Arguments
if (arrArgs.Count = 0) Then								'Display Blurb
	wscript.echo ("Colladd.vbs v1.0")					
	wscript.echo ("03/06/03 Mark Nunn")
	wscript.echo ("http://mark.nunn.net")
	wscript.echo ("Colladd.vbs server filename collectionID - to add from file to collection")
	wscript.echo ("Colladd.vbs server - to list collectionID's")
else

	on error resume next								'Some error handling

	strServer=arrArgs(0)								'set variables from command line

	if (arrArgs.Count = 3) Then
		strCollID = arrArgs(1)
		systemsList = Split(arrArgs(2),";")
	end if
	
	Set objLocator = CreateObject("WbemScripting.SWbemLocator") 			
	Set objSMS = objLocator.ConnectServer(strServer, "Root/SMS")			'connect to sms
	objSMS.Security_.ImpersonationLevel = 3
	wscript.Echo("Connecting to Root/SMS on " & strServer)			
	
	set colSiteDetails=objSMS.ExecQuery("select Machine, SiteCode from SMS_ProviderLocation where ProviderForLocalSite=True")
	For Each insSiteDetails In colSiteDetails
		strSiteCode=insSiteDetails.SiteCode
	next										
	wscript.Echo("Connecting to Root/SMS/site_" & strSiteCode &" on " & strServer)
	set objSMS=objLocator.ConnectServer(strServer, "root/SMS/site_" + strSiteCode)
	wscript.Echo("Connected") 

	if (arrArgs.Count < 3) Then							'if not all arguments supplied list colelctions
		set colCollections=objSMS.ExecQuery("select CollectionID, Name from SMS_Collection ORDER BY CollectionID")
		wscript.echo("CollectionID" & vbTab & "Name")
		For Each insCollection In colCollections
			wscript.echo(insCollection.CollectionID & VbTab & insCollection.Name)
		Next
	else										'otherwise add from file
		set instColl = objSMS.Get("SMS_Collection.CollectionID="&"""" & strCollID & """")
		if Instcoll.Name="" then						'check valid collection
			wscript.echo (strCollId &" Not Found")
		else
			'loop through system list argument
			Dim counter  
			Dim strMachine   
			For counter = 0 To UBound(systemsList)  
				strMachine = systemsList(counter)

				set colNewResources=objSMS.ExecQuery("SELECT ResourceId FROM SMS_R_System WHERE NetbiosName ='" & strMachine & "'")	
				strNewResourceID = 0  				
				For each insNewResource in colNewResources
					strNewResourceID = insNewResource.ResourceID
				Next
				if strNewResourceID <> 0 then				'if one exists crate a collection rule
					Set instDirectRule = objSMS.Get("SMS_CollectionRuleDirect").SpawnInstance_ ()
					instDirectRule.ResourceClassName = "SMS_R_System"	
					instDirectRule.ResourceID = strNewResourceID
					instDirectRule.RuleName = "Findres - Replaces " & strResource
					instColl.AddMembershipRule instDirectRule , SMSContext
					instColl.RequestRefresh False
					wscript.echo(strMachine & " Added to " & Instcoll.Name)
				else
					wscript.echo(strMachine & " Not Found")		'otherwise display error
				end if
			Next  
		end if
	end if
end if
                                  

Open in new window

0
 
LVL 1

Author Comment

by:AnthonySmithMCP
ID: 38717053
Thanks ill try this when I get home.
0
 
LVL 1

Author Comment

by:AnthonySmithMCP
ID: 38723541
Worked Like a charm!! thanks!!!
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question