rtruss
Posts: 230
Score: 10 Joined: 11/4/2004 Status: offline
|
when you run the script does it show you a list of file names? I may have forgot to mention that the script and the txt file need to be in the same location unless you change the following line(s). Change the sscriptpath in the lines below to a location you would store the txt files in. We keep the script and the txt files in the same folder on our sms server. I also updated it to let you know if it did not find any collection files to work with. If oFSO.FolderExists(sScriptPath) Then Set oFolder = oFSO.GetFolder(sScriptPath) For Each oFile In oFolder.Files If LCase(Right(oFile.Name,4))=".txt" Then Set f = oFSO.GetFile(sScriptPath & "\" & oFile.Name) If f.size > 0 Then Redim preserve aFoundFiles(idx) aFoundFiles(idx)=oFile.Name msg = msg & idx & " " & aFoundFiles(idx) & Vbcrlf idx=idx + 1 End If End If Next End If --------------------- updated script ------------------------------- Set WshNetwork = WScript.CreateObject("WScript.Network") sScriptName = WScript.ScriptFullName sScriptPathLen = InStrRev(sScriptName,"\",-1,1) sScriptPath = Left(sScriptName,sScriptPathLen - 1) sTrimmedScriptName = Right(sScriptName, Len(sScriptName)-2) nPos1 = Instr(1,sTrimmedScriptName,"\",1) sSMSServerName = Left(sTrimmedScriptName,nPos1-1) Dim lLocator, gService, ResID, User, aSystems Dim CollectRle, oCollectionSet, oCollection Dim wshShell, oFSO, TrackVar, ts, s Dim SMSServer, SMSSiteCode Dim sCollectionName, sListName, nNameLength Const ForReading = 1 Const ForWriting = 2 Const ForAppending = 8 sInstallName = " SMS Collection Creator" SMSServer = InputBox("Enter the HostName of the SMS Server to work with." & Vbcrlf & Vbcrlf & "Type 'Quit' to end the program." , sInstallName,sSMSServerName) Select Case(LCase(SMSServer)) case "bicsms1" SMSSiteCode = "bic" case "capsar" SMSSiteCode = "cap" case "fdlsms4" SMSSiteCode = "fdl" case "jrzsar" SMSSiteCode = "jrz" case "miasar" SMSSiteCode = "mia" case "stwsms" SMSSiteCode = "PAN" case "pansar" SMSSiteCode = "pan" case "sipsar" SMSSiteCode = "sip" case "stcsar" SMSSiteCode = "stc" case "torsar" SMSSiteCode = "tor" case "tulsar" SMSSiteCode = "tul" case "quit" Msg = "Program terminated upon user request." title = sInstallName & " Notification!" MsgBox msg, 48, title wscript.Quit case "exit" Msg = "Program terminated upon user request." title = sInstallName & " Notification!" MsgBox msg, 48, title wscript.Quit case "" Msg = "Program terminated upon user request or" & Vbcrlf & Vbcrlf Msg = Msg & "by clicking 'OK' without a hostname or" & Vbcrlf & Vbcrlf Msg = Msg & "by clicking 'CANCEL'." & Vbcrlf & Vbcrlf title = sInstallName & " Notification!" MsgBox msg, 16, title wscript.Quit ' case smsserver ' smssitecode = Left(smsserver,3) case else Msg = "I do not recognize " & SMSServer & " as a valid SMS Server." & Vbcrlf & Vbcrlf Msg = Msg & "Please check the name and try again." & Vbcrlf & Vbcrlf title = sInstallName & " Notification!" MsgBox msg, 16, title wscript.Quit end select Set wshShell = CreateObject("Wscript.Shell") Set oFSO = CreateObject("Scripting.FileSystemObject") sScriptName = WScript.ScriptFullName sScriptPathLen = InStrRev(sScriptName,"\",-1,1) sScriptPath = Left(sScriptName,sScriptPathLen - 1) msg = "Please enter the number of the .txt file you want to use." & Vbcrlf & Vbcrlf & "NOTE: The text file name needs to MATCH the collection you are adding to."& Vbcrlf & Vbcrlf Dim aFoundFiles() Redim preserve aFoundFiles(0) idx = 0 notfound = 0 If oFSO.FolderExists(sScriptPath) Then Set oFolder = oFSO.GetFolder(sScriptPath) For Each oFile In oFolder.Files If LCase(Right(oFile.Name,4))=".txt" Then Set f = oFSO.GetFile(sScriptPath & "\" & oFile.Name) If f.size > 0 Then Redim preserve aFoundFiles(idx) aFoundFiles(idx)=oFile.Name msg = msg & idx & " " & aFoundFiles(idx) & Vbcrlf idx=idx + 1 else notfound = notfound + 1 End If else notfound = notfound + 1 End If Next if notfound > 0 then WshShell.Popup "No collection files to work with." &vbcrlf&vbcrlf&"Make sure your file name is correct and try again.",30, sInstallName & " Notice", 48 wscript.quit End If nListName = InputBox(msg, sinstallname & " Collections List") msg = "" If nListName = "" Then WScript.quit sListName = aFoundFiles(nListName) sYear = Year(Now) sMonth = Month(Now) If len(sMonth) = 1 Then sMonth = "0" & sMonth sDay = Day(Now) If len(sDay) = 1 Then sDay = "0" & sDay sDateLogged = sYear& "-" & sMonth & "-" & sDay nHour = Hour(Now) nMin = Minute(Now) nSec = Second(Now) LogFile = sScriptPath & "\" & sDateLogged & "--" & nHour & "." & nMin & "." & nSec & "--" & SMSSiteCode & ".log" nNameLength = Len(sListName) - 4 sCollectionName = Left(sListName,nNameLength) Set oLogFile = oFSO.OpenTextFile(LogFile, ForAppending, True) nCount = 0 If oFSO.FileExists(sScriptPath & "\" & sListName) Then Set ts = oFSO.OpenTextFile(sScriptPath & "\" & sListName, ForReading) oLogFile.WriteLine "List file " & sListName & " was opened successfully. " & now Do While ts.AtEndOfStream <> True sRawLine = ts.ReadLine sRawLine = trim(sRawLine) sRawLine = CStr(sRawLine) nSpacePos = Instr(sRawLine," ") If nSpacePos > 0 Then sRawLine = left(sRawLine,nSpacePos - 1) End If nCommaPos = Instr(sRawLine,",") If nCommaPos > 0 Then sRawLine = left(sRawLine,nCommaPos - 1) End If nDashPos = Instr(sRawLine,"-") If nDashPos > 0 Then sRawLine = left(sRawLine,nDashPos - 1) End If nTabPos = Instr(sRawLine," ") If nTabPos > 0 Then sRawLine = left(sRawLine,nTabPos - 1) End If If len(sRawLine) > 1 Then sMachName = sRawLine TrackVar = "False" Set lLocator = CreateObject("WbemScripting.SWbemLocator") Set gService = lLocator.ConnectServer(SMSServer, "Root\SMS\Site_" & SMSSiteCode) Set aSystems = gService.ExecQuery("Select * From SMS_R_System WHERE Name LIKE ""%" + sMachName + "%""") For Each system In aSystems If UCase(system.name) = UCase(sMachName) Then ResID = system.ResourceID End If Next Set aSystems = gService.ExecQuery("Select * From SMS_R_System WHERE Name LIKE ""%" + sMachName + "%""") For Each system In aSystems If UCase(system.name) = UCase(sMachName) Then ResID = system.ResourceID End If Next '*************************************************************************** '**** Start by spawning a blank instance of a collection rule '**** Then give that instance the values it needs - this is important '*************************************************************************** Set CollectRle = gService.Get("SMS_CollectionRuleDirect").SpawnInstance_() CollectRle.ResourceClassName = "SMS_R_System" CollectRle.RuleName = "ResourceID=" & ResID CollectRle.ResourceID = ResID Set oCollectionSet = gService.ExecQuery("Select * From SMS_Collection") '*************************************************************************** '**** Walk through the enumeration that was returned from the collection . '**** Then query above and select the one provided as a parameter '*************************************************************************** TrackVar = "False" For Each oCollection In oCollectionSet If UCase(oCollection.Name) = UCase(sCollectionName) Then oCollection.AddMembershipRule CollectRle If Err.Number = 0 Then TrackVar = "True" End If End If Next '*************************************************************************** '**** Update log file below with success/Failure message, then end script '**** Place the path to your own log file here in UNC format. Name it something like ‘*** add2Coll.log '*************************************************************************** If TrackVar = "True" Then nCount = nCount + 1 oLogFile.WriteLine sMachName & " was added successfully." sSuccessList = sSuccessList & nCount & " - " & sMachName & " was added successfully." & Vbcrlf Else oLogFile.WriteLine sMachName & " was NOT added successfully." End If ' End If Loop Else WScript.echo "File not found: " & sScriptPath & "\" & sListName WScript.Quit End If Set oLogFile = Nothing Set ts = Nothing msg = "" msg = "Script Completed." & Vbcrlf msg = msg & sCollectionName & Vbcrlf msg = msg & "Total Number Added: " & nCount & Vbcrlf msg = msg & sSuccessList WScript.echo msg wscript.quit
_____________________________
Roger Truss Assistant Windows Administrator SMS Admin Trend Micro Admin ImageUltra Admin Please rate my post ;)
|