lramsdale
Posts: 1068
Score: 44 Joined: 1/23/2004 From: Oxford - UK Status: offline
|
yes there is a vbs script called colladd that does exactly this: quote:
' ################################################################# ' #colladd.vbs http://mark.nunn.net # ' # # ' #This adds a list of machine names from a file to a collection. # ' #It can also list all collections on a server. # ' ################################################################# ' #M.Nunn 03/06/03 # ' ################################################################# ' #Update 04/11/03: Fixed bug with rule name (thanks to Lance # ' #Rissman) # ' ################################################################# Set fso = CreateObject(" Scripting.FileSystemObject" ) strBlurb= " Colladd.vbs v1.1" & VbCrLf & _ " 03/06/03 - Updated 04/11/03 Mark Nunn" & VbCrLf & _ " http://mark.nunn.net" & VbCrLf & _ " Colladd.vbs server filename collectionID - to add from file to collection" & VbCrLf & _ " Colladd.vbs server - to list collectionID' s" intDisplayLines=30 ' Number of lines to display in each page under Wscript Set arrArgs = WScript.Arguments if (arrArgs.Count = 0) Then ' Display Blurb wscript.echo (strBlurb) 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 strScriptPath = WScript.FullName Set regEx = New RegExp ' Discover if running from Wscript regEx.Pattern = " Wscript" ' All display in one go if it is regEx.IgnoreCase = True ' line by line if it isn' t booWscript = regEx.test(strScriptPath) Set objLocator = CreateObject(" WbemScripting.SWbemLocator" ) Set objSMS = objLocator.ConnectServer(strServer, " Root/SMS" ) ' connect to sms objSMS.Security_.ImpersonationLevel = 3 if not booWscript then 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 if not booWscript then wscript.Echo(" Connecting to Root/SMS/site_" & strSiteCode &" on " & strServer) set objSMS=objLocator.ConnectServer(strServer, " root/SMS/site_" + strSiteCode) if not booWscript then 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 Name" ) if not booWscript then wscript.echo(" CollectionID" & vbTab & " Name" ) else strOutput=strOutput & " CollectionID" & vbTab & " Name" & VbCrLf end if For Each insCollection In colCollections if not booWscript then wscript.echo(insCollection.CollectionID & VbTab & insCollection.Name) else strOutput=strOutput & insCollection.CollectionID & VbTab & insCollection.Name & VbCrlf end if 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, Name FROM SMS_R_System WHERE NetbiosName =' " & strMachine & " ' " ) strNewResourceID = 0 For each insNewResource in colNewResources strNewResourceID = insNewResource.ResourceID strName = insNewResource.Name 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 = strName & " Added by CollAdd.vbs" instColl.AddMembershipRule instDirectRule , SMSContext instColl.RequestRefresh False if not booWscript then wscript.echo(strName & " Added to " & Instcoll.Name) else strOutput=strOutput & strName & " Added to " & Instcoll.Name & VbCrLf end if else if not booWscript then wscript.echo(strMachine & " Not Found" ) ' otherwise display error else strOutput=strOutput & strMachine & " Not Found" & VbCrLf end if end if WEnd ' next line else wscript.echo (" Can' t Open " & strfile) ' if file not found end if end if end if end if if booWscript and strOutput then ' if running in Wscript display stored output arrOutput=split(strOutput,VbCrLf) strOutput=" " for each strLineout in arrOutput intCount=intCount+1 ' process each line ' wscript.echo (intCount) strOutput=strOutput & strLineout & VbCrLf if intCount = intDisplayLines then wscript.echo (strOutput) ' and when enough lines for a page display them strOutput=" " intCount=0 end if next wscript.echo (strOutput) end if quote:
Collad.VBS ---------- The colladd vbs script will take a list of machines from a text file and add them into an existing collection. To do this: 1) Create a text file contain a list of machine names to add to the collection. This should be in the format: Machine1 Machine2 Machine3 ... 2) run ' colladd.vbs smserver' where smsserver is the netbios name of your SMS server. This will give you a list of all the collection names and collection id' s. Make a note of the collection ID for the collection you want to add the machines to. 3)run ' CollAdd.vbs SMSserver filename CollectionID' where SMSserver is the name of your SMS server, filename is the file with the list of machines and CollectionID is the ID of the Collection you wish to add the machines to. Quick Example: to add three machines to a collection called ' Test Collection' in an SMS server called SMS01 1) create ' machines.txt' containing: FredPC BertPC JohnPC 2) colladd.vbs SMS01 SMS00001 All Systems SMS00002 All Users ... S01000FE Test Collection 3) colladd.vbs SMS01 machines.txt S01000FE FredPC Added to Test Collection BertPC Added to Test Collection JohnPC Added to Test Collection NOTE: This script will behave differently depending on whether it' s running under Cscript or Wscript. When run under Cscript it will display all output a line at a time. Under Wscript it will not display any output until the end of the script, then it will display 30 lines at a time. The script can be configured to display more or less lines to each message in Wscript by changing the line ' intDisplayLines=30' to the required value.
|