'*** EDIT USER'S LOGON SCRIPT BASED ON STARTING STRING IN USERNAME *** 'Copyright 2001, MSD Inc. and Alan Kaplan 'www.msdinc.com 'www.akaplan.com/tools.html 'Written 5/9/01, 6/11/01 'some of this code has been based on the example files 'in Microsoft Windows Script Host 2.0 Developer Guide, by Gunter Born 'You must have ADSI on PC running this script for it to work Option Explicit Dim objLDomain,objContainer,colUsers,objEnv,objDomain Dim isnew, path, newpath, oFolders,fso,userfile,logfile dim appendout,rdname dim message,searchfor,mydomain,ldomain,title, command Dim major,minor,ver,wshshell,key,key2 dim oldscript, newscript, user, regex, retval dim objUser, counter counter = 0 Set WshShell = WScript.CreateObject("WScript.Shell") set objEnv = WshShell.Environment("process") mydomain = objEnv("USERDOMAIN") 'Get current domain name on error resume next 'Check adequacy of current config. This can be commented out to speed 'up processing SysTest If (Not IsCScript()) Then message = "You must set default host to cscript to run this script." &vbcrlf &_ "Use the command wscript //h:cscript" WshShell.Popup message,30,"Workstation Setup Error",vbCritical WScript.Quit ' Terminate script. End If set objEnv = WshShell.Environment("process") 'Get logon domain name for default domain Ldomain = objEnv("USERDOMAIN") 'get resource domain name as default search string within logon domain key = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\Currentversion\Winlogon\CachePrimaryDomain" RDname = WshShell.RegRead (key) ' Define dialog box variables. message = "Logon Domain to Query" title = "Logon Domain" 'get domain name, domain default ldomain = InputBox(message, title, mydomain) ' Evaluate the user input. If ldomain = "" Then ' Canceled by the user WScript.quit End If ' Define dialog box variables. message = "Change User accounts beginning with the following string:" title = "User Name Search Mask" 'get domain name, domain default searchfor = InputBox(message, title, RDname) ' Evaluate the user input. If searchfor = "" Then ' Canceled by the user WScript.quit End If 'setup log on error goto 0 'logfile is set to append. Delete old if doing batch of workstations. logfile = "c:\" & searchfor & "changescript.csv" 'Name of log file. Const ForAppend = 8 set fso = CreateObject("Scripting.FileSystemObject") Dim Hval hval = FALSE ' Check whether the file exists. If (fso.fileExists(logfile)) Then If(MsgBox("Logfile Exists. Delete file " & logfile & " ?" &_ vbcrlf & "(If you press 'No', log will append)" , _ vbYesNo + vbQuestion, Title) = vbYes) Then fso.Deletefile(logfile) hval = TRUE End If else Hval = TRUE End If set AppendOut = fso.OpenTextFile(logfile, ForAppend, True) if hval then 'need header appendout.writeline "Username,OldScript,NewScript,Result,Date" End If ' Define dialog box variables. message = "Please enter name of Login Script to replace." message = message & vbcrlf & vbcrlf & "Blank plus 'OK' or 'Cancel' here will search for empty logon scripts." title = "Old Script" 'get domain name, domain default oldscript = ucase(InputBox(message, title, "smsls.bat")) ' Define dialog box variables. message = "Please enter name of new Login Script." & vbcrlf & vbcrlf &_ "Make sure that the new script exists in NETLOGON Share before you begin!" &_ vbcrlf & vbcrlf & "Blank plus 'OK' or 'Cancel' here will leave users with no logon script." title = "New Script" 'get domain name, domain default newscript = ucase(InputBox(message, title, rdname&".bat")) 'Bind to the builtin administrators group to see if you can connect on error resume next 'This is a kludge, but binding to non-existent domain does not raise error GetObject("WinNT://" & ldomain &"/administrators") if err <> 0 then message = "Cannot bind to " &ldomain WshShell.Popup message,0,"Connection or Authority Failure",vbCritical WScript.Quit End If 'This should work with with Win2k Active directory per docs.... objLDomain = "WinNT://" & ldomain 'Case counts here! ' Define dialog box variables. dim continue message = "Ready to Replace " & oldscript & " with " & newscript & _ vbcrlf & "for Users named " & searchfor & "*" &_ vbcrlf & "Be patient, this may take some time" title = "Ready to Replace" 'get domain name, domain default continue = ucase(msgBox(message,1, title)) If continue = 2 Then msgbox("Cancelled") WScript.quit End If Set objContainer=GetObject(objLDomain) objContainer.Filter=Array("user") For Each user in objContainer MoreTests SearchFor,user.name 'Run MoreTests function Next 'Show done appendout.Close If (MsgBox("Done." & vbcrlf & vbcrlf & counter & " Users named " & searchfor & "* have had " &_ vbcrlf & "login script " & oldscript & vbcrlf & "replaced by " & newscript & _ ". " & vbCrLf & "Open log " & logfile & " ?",_ vbYesNo + vbQuestion,"Script Complete") = vbYes) Then WshShell.Run(logfile) End If Wscript.Quit 'Script ends Function MoreTests(patrn, strng) dim status status = "Ignored" 'checks for pattern in username on error resume next Set regEx = New RegExp ' Create regular expression. regEx.Pattern = patrn ' Set pattern. regEx.IgnoreCase = True ' Set case sensitivity. retVal = regEx.Test(strng) ' Execute the search test. If retVal Then ' if match, setup to write Set objUser = GetObject(objLDomain&"/"& user.name) message = objUser.Name message = message & "," message = message & objUser.LoginScript If ucase(objUser.LoginScript) = oldscript then objUser.LoginScript = newscript 'uncomment if you need it objUser.SetInfo ' Store settings if err <> 0 then status = "Failure" Else status = "Success" counter = counter + 1 end if End If message = message & "," & objUser.LoginScript & "," & status& "," & date echoandlog message End If message = "" End Function Sub EchoAndLog (message) 'Echo output and write to log Wscript.Echo message AppendOut.WriteLine message End Sub Sub SysTest() on error resume next ' WSH version tested Major = (ScriptEngineMinorVersion()) Minor = (ScriptEngineMinorVersion())/10 Ver = major + minor 'Need version 5.5 If err.number or ver < 5.5 then message = "You have WScript Version " & ver & ". Please load Version 5.5" End If 'Test for ADSI err.clear key = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\{E92B03AB-B707-11d2-9CBD-0000F87A369E}\version" key2 = WshShell.RegRead (key) if err <> 0 then message = message & "ADSI must be installed on local workstation to continue" & vbCrLf WshShell.Popup message,0,"Workstation Setup Error",vbInformation WScript.Quit End if End Sub Function IsCScript() ' Check whether CScript.exe is the host. If (InStr(UCase(WScript.FullName), "CSCRIPT") <> 0) Then IsCScript = True Else IsCScript = False End If End Function '*** SCRIPT ENDS ****