'Login Local User Script 'Author: Felipe Ferreira fferreira@osiatis.es 'Date: 06/03/2008 'Version: 3.0 '2.0 Updates: 'Use directly USERNAMES (faster) - OK 'Use sys variable to check if in DOmain - OK 'Try to integrate RunAs and - OK 'Password Encryption - OK 'Set Company WallPaper - refresh screen - OK 'POP up should not stop the script from running - OK '3.0 Updates: 'Sync with Server, only if in server is newer - OK 'Sync with Server SPecial ServerName FOlder - OK 'Sync itself Login.vbs if Server is new = OK 'Log Synced Files to c:\maq.txt - OK 'Verify that LU.reg exists, if not Logoff - OK 'Funcionamiento del Script '1. Verifica el nombre del usuario que hecho el login. Verifica si hecho login local o en el servidor. Verifica si existe el archivo LU.reg*, si no existe hace un logout! '2. Intenta sincronizar con la carpeta* en el server. Hace log de los archivos sincronizados en c:\maq.txt verifica si el usuario no hecho login local después salí del script. Porque el usuario esta en el dominio, entonces las directivas de Dominio funcionara. Si la carpeta remota no es accesible continúa con el script. '3. Envía una mensaje al usuario avisando que el ordenador si esta actualizando. Cerra el explorer.exe, Aplica los cambios de registro basado en el usuario ( sala, adminbcn, administrador) '4. Envía otra mensaje avisando que la actualización estado terminada. Reinicia explorer.exe '*Ésos son variables y pueden ser fácilmente cambiados '---------------------------------------------GLOBAL VARIABLES----------------------------------------------------------------------- Dim strUser,strHost, strLogonServer Dim strPingit Dim logfile Dim objGroupDict Dim strWindir,strLocalDrive Dim sPass1, sPass2, sPass3 Dim h : h = 0 'for log, to run initial date, info, just once Dim bLoginLocal : bLoginLocal = False 'Boolean to check if login Local or Not Dim fs : Set fs = CreateObject("Scripting.FileSystemObject") Dim WSHNetwork : Set WSHNetwork = Wscript.CreateObject("Wscript.Network") dim oShell : Set oShell = WScript.CreateObject("WScript.Shell") Dim strScriptFile : strScriptFile = WScript.ScriptFullname Dim sPath : sPath = Left(strScriptFile, Len(strScriptFile) - Len(WScript.Scriptname)) 'used to define the path from where the script file is located '---------------------------------------------PRE SET VARIABLES----------------------------------------------------------------------- Dim bVerbose : bverbose = False ' 1 for Debug Mode Dim oSyncFolder : oSyncFolder = "\\d_corpo\dadescomubcnact$\BcnActLogon\RO\Aplicacions\policeCorpoPortatils\comu\" Dim RegSala : RegSala = sPath & "LU.reg" '.REG FOR SALA USER, LU = LocalUser Dim RegAdminBCN : RegAdminBCN = sPath & "LA.reg" '.REG FOR ADMINBCN USER, LA = Local Admin sPass1 = Decrypt("111123mbt") 'sala password encrypted sPass2 = Decrypt("654332neb") 'adminbcn password encrypted sPass3 = Decrypt("432ov32") 'runas password encrypted '_____________________________MAIN__________________________________________ on error resume next '---->Check what kind of Group and Username and SET strUserType getUserInfo '---->Check if Domain Network is UP, IF up then UPDATE and Quit Script ---> USER LOGONSERVER variable!!! CheckNetwork '----->Try to Sync to Server folder if not reacheable just go on, if sync is OK and user not LoginLocal then QUIT SCRIPT Sync '---->PopUp msg and Kill Explorer call popup("Actualizando...", 5) call exec("cmd /c TASKKILL /F /IM explorer.exe") '---->Depending on UsersGroup Call .reg to Update Registry if strUser = "sala" then call RunAs("runas",sPass3,"net localgroup Administradores sala /add") wscript.sleep 1200 call RunAs(strUser,sPass1,"regedit /s " & RegSala) wscript.sleep 1250 call RunAs("runas",sPass3,"net localgroup Administradores sala /delete") elseif strUser = "adminbcn" then CleanRegistry ' call RunAs(strUser,sPass2, sPath & " & RegAdminBcn) 'AUN NO TENGO LA.REG elseif strUser = "Administrador" then CleanRegistry elseif strUser = "t3" then call RunAs("runas",sPass3,"net localgroup Administradores t3 /add") wscript.sleep 1200 call RunAs(strUser,"t3","regedit /s " & RegSala) wscript.sleep 1250 ' call RunAs("runas",sPass3,"net localgroup Administradores t3 /delete") end if '---->Start Again Explorer.exe and Update GPOs call exec("c:\iniApli.exe %windir%\explorer.exe") call exec("cmd /c gupdate /force") 'call exec("%comspec% /c explorer.exe") call exec("RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters") 'redraw WALLPAPER 'Clean popup temp Set c = fs.GetFile("c:\temp\temp.vbs") c.delete wscript.quit(0) '_____________________________END MAIN__________________________________________ '===================START FUNCTIONS AND SUBS===================================================== Sub getUserInfo 'CHECKS USERS GROUPS Set strUserType depending on User Group strUser = "" strHost = "" While strUser = "" wscript.sleep 5 strUser = WSHNetwork.Username Wend call displayMsg("USERNAME = " & struser) strHost = WSHNetwork.UserDomain call displayMsg("HOSTNAME = " & strHost) end sub Sub CleanRegistry 'Deletes all Policy related folders, call exec("reg delete HKEY_LOCAL_MACHINE\SOFTWARE\Policies\ /f") call exec("reg add HKEY_LOCAL_MACHINE\SOFTWARE\Policies\ /f") call exec("reg delete HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\policies /f") call exec("reg add HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\policies /f") call exec("reg delete HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies /f") call exec("reg add HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies /f") call exec("reg delete HKEY_CURRENT_USER\Software\Policies /f") call exec("reg add HKEY_CURRENT_USER\Software\Policies /f") end sub function exec(execCmd) 'SHOULD USE RUN SO WE CAN HIDE THE POPUP WINDOW! on error resume next dim strCmd dim WshShell3 : set WshShell3 = CreateObject("WScript.Shell") call DisplayMsg( execCmd ) ex=WshShell3.Run(execCmd,0, FALSE) set ex=nothing end function Function RunAs(sUser,sPass,sCmd) 'NOT WORKING!!! :( On Error Resume Next dim WshShell, FSO,WinPath set WshShell = CreateObject("WScript.Shell") set WshEnv = WshShell.Environment("Process") WinPath = WshEnv("SystemRoot")&"\System32\runas.exe" set FSO = CreateObject("Scripting.FileSystemObject") if FSO.FileExists(winpath) Then 'wscript.echo winpath & " " & "verified" Else CAll DisplayMsg("!! ERROR !!" & VBCRLF & "No ecuentro " & winpath) set WshShell=Nothing set WshEnv=Nothing set FSO=Nothing wscript.quit end if call DisplayMsg("runas /user:" & sUser & " " & CHR(34) & sCmd & CHR(34)) ruc=WshShell.Run("runas /user:" & sUser & " " & CHR(34) & sCmd & CHR(34), 2, FALSE) 'need to give time for window to open. do until WshShell.AppActivate(WinPath) 'make sure we grab the right window to send password to Wscript.Sleep 15 WshShell.AppActivate(WinPath) loop WshShell.SendKeys sPass &VBCRLF 'call DisplayMsg("RunAs is done") 'send the password to the waiting window. set WshShell=Nothing set WshEnv=Nothing set FSO=Nothing end Function Sub CheckNetwork Set objShell = CreateObject("WScript.Shell") ' Run cmds 'Get LogonServer Set objExecObject = objShell.Exec("cmd /c echo %LOGONSERVER%") Do While Not objExecObject.StdOut.AtEndOfStream strLogonServer = objExecObject.StdOut.ReadLine() loop 'CLEAN UP THE LOGONSERVER NAME, remove \\ strLogonServer = replace(strLogonServer,"\\","") strLogonServer = trim(strLogonServer) 'GET WINDIR Set strWindir = fs.GetSpecialFolder(0) 'SET GLOBAL VARIABLES strWindir = trim(strWindir) & "\" strLocalDrive = Left(strWindir,3) logfile = strLocalDrive & "maq.txt" DisplayMsg strWindir & strLocalDrive & logfile If (strHost = "") Then Set objExecObject = objShell.Exec("cmd /c echo %COMPUTERNAME%") Do While Not objExecObject.StdOut.AtEndOfStream strHost = objExecObject.StdOut.ReadLine() loop End If if strLogonServer <> strHost then displaymsg "Login in Domain!" bLoginLocal = False elseif strLogonServer = strHost then displaymsg strUser & " login Local " &strHost & " = " & strLogonServer bLoginLocal = True end if 'CONTROLS THAT THE .REG FILE EXISTS, 'if .reg file not found, LOGOUT AND QUIT SCRIPT! If fs.FileExists(RegSala) = False Then DisplayMsg "ERROR: Archivo " & regsala & " no encuentrado!!!" call exec("%comspec% /c logoff.exe") wscript.quit(0) Exit sub end if 'Set objExecObject = nothing end sub Sub Sync 'Checks If Sync Folder is Reacheable , Confirm it is in Domain Network If fs.FolderExists(oSyncFolder) = False Then Call DisplayMsg( "Caprtea per Sync no encuentrada!") 'User is NOT in Network Exit sub else Call DisplayMsg( "Caprtea per Sync OK, Actualizando...") 'IF FINDS THE FOLDER WITH SAME AS COMPUTER NAME THEN UPDATE ALL FILES FROM THERE If fs.FolderExists(oSyncFolder & strHost) then oSyncFolder = oSyncFolder & strHost & "\" end if Set fs = nothing 'Start Copying/updating Call CopyFiles(oSyncFolder & "windows\",strWinDir) Call CopyFiles(oSyncFolder & "system32\" ,strWinDir & "system32\") Call CopyFiles(oSyncFolder & "C\",strLocalDrive) Call CopyFiles(oSyncFolder & "reg\",sPath) Call CopyFiles(oSyncFolder & "scripts\",sPath) if bLoginLocal = false then 'User is in DOMAIN and Sync has finished DisplayMsg "User is in DOMAIN and Sync has finished, Quiting..." wscript.quit(0) end if end if end Sub Function CopyFiles(folderspec1,folderspec2) '----------COPY BUT FIRST COMPARE IF EXISTS AND IF SO COMPARE IF NEWER -------- on error resume next Dim f,f1,fc,strFileName1,intFileSize1,intFileDate1 Dim strFile2,strFileName2,intFileSize2,intFileDate2,TFile Dim objFileCopy Set fs = CreateObject("Scripting.FileSystemObject") TFile = 0 'Lo script gira solo se la folder esiste 'DisplayMsg "Copying/Updating From: " & folderspec1 & " to " & folderspec2 If (fs.FolderExists(folderspec1)) and (fs.FolderExists(folderspec2)) Then Set f = fs.GetFolder(folderspec1) Set fc = f.Files 'GOES THRU EACH FILE IN SOURCE FODLER For Each f1 in fc strFileName1=f1.name intFileSize1=f1.size intFileDate1 = f1.DateLastModified 'Gets Last Updated 'CHECK IF FILE EXISTS IN TARGET DIRECTORY If fs.FileExists(folderspec2 & strFileName1) Then Set strFile2 = fs.GetFile(folderspec2 & strFileName1) strFileDate2 = strFile2.DateLastModified strFileName2 = strFile2.Name If intFileDate1 > strFileDate2 Then Set objFileCopy = fs.GetFile(folderspec1 & strFileName1) objFileCopy.Copy (folderspec2) DisplayMsg "Actualizando : " & objFileCopy.name & " - " & intFileDate1 &_ " to " & folderspec2 & objFileCopy.name & " - " & intFileDate1 call Log(logfile, "Actualizando : " & objFileCopy.name & " - " & intFileDate1 &_ " to " & folderspec2 & objFileCopy.name & " - " & intFileDate1) TFile = TFile+1 end if else ' IF FILE DOES NOT ALREADY EXIST, COPY SOURCE FILE TO TARGET Set objFileCopy = fs.GetFile(folderspec1 & strFileName1) objFileCopy.Copy (folderspec2) DisplayMsg "Copiando : " & objFileCopy.name & " to " & folderspec2 & objFileCopy.name call Log(logfile, "Copiando : " & objFileCopy.name & " to " & folderspec2 & objFileCopy.name) TFile = TFile+1 end if set objFileCopy = nothing Next End If 'Print counters and do additions for each folder if TFIle <> 0 then DisplayMsg TFile & " archivos copiados." call log (logfile, TFile & " archivos copiados.") end if set fs = nothing End Function Function Log(Outputfile, msgLine) Dim oFSO : Set oFSO = CreateObject("Scripting.FilesyStemObject") Const ForAppending = 8 Set ofile = oFso.OpenTextFile(Outputfile, ForAppending, TRUE) if h = 0 then oFile.Writeline VbCrLf & "#################Portatil Login Sync##################" oFile.Writeline "Usuario: " & strUser & VbCrLf &"Nombre Poratil: " & strHost oFile.Writeline "Script: " & strScriptFile & " el " & Date & " las " & Time & VbCrLf h = h + 1 end if oFile.Writeline msgLine Set oFSO = nothing end function Function popup(msgPop, isec) 'Create a temp vbs and do the popup, so it does not stop the current running script, should delete it too! Set fs = CreateObject("Scripting.FileSystemObject") dim WshShell2 : set WshShell2 = CreateObject("WScript.Shell") set b=fs.CreateTextFile("c:\temp\temp.vbs") msgPop = chr(34) & msgPop & chr(34) b.writeline "set WshShell = WScript.CreateObject( ""WScript.Shell"" )" b.writeline "ret = WshShell.Popup( " & msgpop & ", "& isec & ", " & msgpop & ", vbInformation )" b.close rc=WshShell2.Run("CSCRIPT /NoLogo c:\temp\temp.vbs",0, FALSE) wscript.sleep 20 set rc = nothing end function Function Decrypt(Encryptedstring) 'Could use a method to check script file size, if changed do not Decrypt! (?) Dim x, i, tmp encryptedstring = StrReverse( encryptedstring ) For i = 1 To Len( encryptedstring ) x = Mid( encryptedstring, i, 1 ) tmp = tmp & Chr( Asc( x ) - 1 ) Next Decrypt = tmp End Function Function Encrypt(String) Dim x,i,tmp For i = 1 to Len(String) x = Mid(String,i,1) tmp = tmp & Chr ( Asc (X) + 1) next tmp = strReverse(tmp) Encrypt = tmp end function Function DisplayMsg(msgTxt) 'Prints msg on screen only if Verbose is set if bVerbose = True then wscript.echo msgTXT end if end Function