'$$$$ Version 2006-10-11 $$$$ 'Compiled/Written by Johan Greefkes, 2004-2006 'Use it, change it, forward it... I don't care. Just don't hold me resposible! ' 12:32 PM 1/20/2005: ADD: WriteLog() function. See Usage below ' 12:25 PM 1/20/2005: ADD: Print successfull isMember() functions to the log file. ' 12:01 AM 7/24/2005: ADD: isComputerMember function Check group membership for computers ' 1:07 PM 7/24/2005: MOD: Cleaned up code a bit, properly empty objects and some other minor changes ' 1:20 PM 7/24/2005: ADD: Run Command: Execute external files ' 1:31 PM 7/24/2005: ADD: FileCopy Command: Execute external files ' 10:17 PM 7/26/2005: FIX: Driveletter remap issue in the DriveMapper sub ' 4:40 PM 7/27/2005: FIX: Error handling in isComputerMember function in case group referred does not exist. ' 10:44 PM 10/7/2005: ADD: Support for Cross Domain Authentication. Set bolEnableCrossDomain to True below. ' I have only tested this on Windows 2000, XP and 2003. Win9x is an unknown '13:41 PM 10/10/2005: FIX: Option Explicit revealed some undefined variables. ' 3:16 PM 6/12/2006: ADD: RemovePrintersFromServer to remove all printers connected to a server ' 9:15 PM 10/11/2006: ADD: isThisIP function to match against active IP addresses on the computer. Option Explicit Dim strLogName, bolWriteLog, bolManagePrinters, bolEnableCrossDomain strLogName = "script.log" ' Filename of log to write to in the temp folder. bolWriteLog = True ' Toggles log-writing. I like it on, you may like it off. bolManagePrinters = True ' Windows 9x doesn't do very well with printer management, set this to false to disable bolEnableCrossDomain = True ' Toggles Cross Domain Authentication. Reads registry to obtain NetBIOS domain for the machine. 'Script Begins Here Dim objNetwork, objDrives, objDict, objComputer, objShell, objFileSystem, objLogFile, objRootDSE, objTrans Dim strComputerName, strDomain, strUser, strAdsPath, strNetBIOSDomain, strDNSDomain, strTemp, strLogPath, i Set objNetwork = WScript.CreateObject("Wscript.Network") Set objDrives = objNetwork.EnumNetworkDrives Set objShell = WScript.CreateObject("WScript.Shell") strComputerName = objNetwork.ComputerName strDomain = objNetwork.UserDomain strUser = objNetwork.UserName strAdsPath = strDomain & "/" & strUser If bolEnableCrossDomain then on error resume next strNetBIOSDomain = objShell.RegRead _ ("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultDomainName") if not err.number = 0 then msgbox "Problem reading the computer's domain name from the Registry." & chr(10) & _ "Please set ""bolEnableCrossDomain"" to False", vbCritical, "Logon Script Error" wscript.quit end if on error goto 0 else strNetBIOSDomain = strDomain end if Set objComputer = GetObject("WinNT://" & strNetBIOSDomain & "/" & strComputerName & ",computer") strTemp = objshell.ExpandEnvironmentStrings("%TEMP%") strLogPath = strTemp & "\" & strLogName '" set objFileSystem = WScript.CreateObject("Scripting.FileSystemObject") if bolWriteLog then 'initialize log file on error resume next Set objLogFile = objFileSystem.OpenTextFile(strLogPath, 2, True) '" writelog "############### Start Login Script ##################" if not err.number = 0 then msgbox "There was a problem opening the log file for writing." & chr(10) & _ "Please check whether """ & strLogPath & """ is a valid file and can be openend for writing." & _ chr(10) & chr(10) & "If you're not sure what to do, please contact your support person.",vbCritical, "Logon Script Error" wscript.quit end if on error goto 0 end if '############################### ############################### '############################### INSTRUCTIONS ############################### '############################### ############################### 'Check for user group membership with isMember: 'isMember will write an entry in the log file when a user is in the group 'if isMember("groupname") then ' commands here 'end if 'Check for computer group membership with isMember: (usefull for location specific printer creation) 'isComputerMember will write an entry in the log file when a user is in the group 'if isComputerMember("groupname") then ' commands here 'end if 'Check the computer running the script for a specific IP address with isThisIP: 'if isThisIP("172.23.0.5") then ' commands here 'end if 'isThisIP will write log file entries when logging is enabled. Requires WMI. 'Map drives with DriveMapper: 'DriveMapper "Drive:", "\\Server\share" 'Add Printers with AddPrinter: 'AddPrinter "\\Server\Printername" 'Set a default printer with SetPrinterDefault (printer has to exist, or this will not work) 'SetPrinterDefault "\\Server\Printername" 'Remove Printers with RemovePrinter: 'RemovePrinter "\\Server\Printername" 'Remove All Printers from a Server with RemovePrintersFromServer. Handy when you retire a windows print server. 'RemovePrintersFromServer "Servername" 'Execute any file with Run: 'Run "c:\windows\notepad.exe" 'Run "\\server\share\script.vbs" 'Copy files with FileCopy(source, target): 'FileCopy "\\server\share\*.txt", "c:\temp\" 'NOTE: copies are not recursive, and wildcards can only exists in the file level. 'NOTE: Users must have permission to write files in the target location 'Write an entry to the log file with WriteLog(): 'writelog("some text here") 'Set user environment variables with PutEnv: 'PutEnv "Variablename", "VariableValue" 'A log file will be placed in the user's temp directory and will 'be named to the ScriptLogName variable set above. '############################### CHANGE ############################### '############################### SCRIPT ############################### '############################### BELOW ############################### If IsComputerMember("CitrixServers") Then bolManagePrinters = False writeLog "PrinterManagement Disabled for Citrix Servers" ' Exclude anyone logging on to citrix server from creating printers. End If if isMember("Sales") then DriveMapper "h:", "\\salesserver\home\" & strUser '"'Connect H: to \\salesserver\home\johan DriveMapper "i:", "\\salesserver\salesdept" ' Connect I: to \\salesserver\salesdept AddPrinter "\\salesserver\laserjet" ' Create printer from shared printer SetPrinterDefault "\\salesserver\laserjet" ' Set the printer as default end if '######################### ^^CHANGE SCRIPT ABOVE^^ ########################## '######################### Subs 'n Functions Below ######################### '############################## DO NOT CHANGE ############################## writelog "################ End Login Script ###################" Set objNetwork = Nothing Set objDrives = Nothing Set objComputer = Nothing Set objShell = Nothing Set objFileSystem = Nothing Set objLogFile = Nothing Wscript.Quit Function IsMember(sGroup) Dim oDict, oUser, oGroup If IsEmpty(oDict) Then Set oDict = CreateObject("Scripting.Dictionary") oDict.CompareMode = vbTextCompare Set oUser = GetObject("WinNT://" & strAdsPath & ",user") For Each oGroup In oUser.Groups oDict.Add oGroup.Name, "-" Next Set oUser = Nothing End If IsMember = CBool(oDict.Exists(sGroup)) if IsMember and bolWriteLog then objLogFile.WriteLine(Now() & ": Success: User is member of " & sGroup) End Function Function IsComputerMember(sGroup) Dim oGroup on error resume next Set oGroup = GetObject("WinNT://" & strDomain & "/" & sGroup & ",group") IsComputerMember = CBool(oGroup.IsMember(objComputer.ADsPath & "$")) if IsComputerMember and bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Computer is member of " & sGroup) Set oGroup = Nothing If not Err.Number = 0 Then if bolWriteLog then objLogFile.WriteLine(Now() & ": Failed : IsComputerMember could not locate group " & sGroup) end if on error goto 0 End Function sub WriteLog(sEntry) if bolWriteLog then objLogFile.WriteLine(Now() & ": Log: " & sEntry) End Sub Sub DriveMapper(sDrive, sShare) For i = 0 to objDrives.Count -1 Step 2 if LCase(sDrive) = LCase(objDrives.Item(i)) then if not LCase(sShare) = LCase(objDrives.Item(i+1)) then objNetwork.RemoveNetworkDrive sDrive, true, true Else if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Connect """ & sDrive & """ to share """ & _ sShare & """ (previously connected)") Exit Sub End if End if Next on error resume next objNetwork.MapNetworkDrive sDrive, sShare If Err.Number = 0 Then if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Connect """ & sDrive & """ to share """ & sShare & """") else if bolWriteLog then objLogFile.WriteLine(Now() & ": Failed: Connect """ & sDrive & """ to share """ & sShare & """") end if on error goto 0 End Sub sub AddPrinter(sPrinterUNC) on error resume next if not bolManagePrinters then if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Printer connection skipped because disabled (" & _ strUser & " on " & strComputerName & ")") else objNetwork.AddWindowsPrinterConnection sPrinterUNC If Err.Number = 0 Then if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Connect to printer: " & sPrinterUNC) else if bolWriteLog then objLogFile.WriteLine(Now() & ": Failed: Connect to printer: " & sPrinterUNC) end if end if on error goto 0 end sub sub RemovePrinter(sPrinterUNC) dim oPrinters Set oPrinters = objNetwork.EnumPrinterConnections For i = 0 to oPrinters.Count - 1 Step 2 if uCase(oPrinters.Item(i+1)) = uCase(sPrinterUNC) then on error resume next objNetwork.RemovePrinterConnection sPrinterUNC, true, true If Err.Number = 0 Then if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Remove printer: " & sPrinterUNC) else if bolWriteLog then objLogFile.WriteLine(Now() & ": Failed: Remove printer: " & sPrinterUNC) end if on error goto 0 exit sub end if Next if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Remove printer: " & sPrinterUNC & " (Printer did not exist)" ) set oPrinters = Nothing end sub sub RemovePrintersFromServer(sServerName) dim oPrinters, aPrinter if bolWriteLog then objLogFile.WriteLine(Now() & ": Removing all printers from: " & ucase(sServerName)) Set oPrinters = objNetwork.EnumPrinterConnections For i = 0 to oPrinters.Count - 1 Step 2 on error resume next aPrinter = split(uCase(oPrinters.Item(i+1)),"\",-1, 1) '" if not UBound(aPrinter) = 0 then 'this is not a local printer, process code if aPrinter(2) = ucase(sServerName) then objNetwork.RemovePrinterConnection oPrinters.Item(i+1), true, true If Err.Number = 0 Then if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Remove printer: " & oPrinters.Item(i+1)) else if bolWriteLog then objLogFile.WriteLine(Now() & ": Failed: Remove printer: " & oPrinters.Item(i+1)) end if end if end if aPrinter = "" on error goto 0 next if bolWriteLog then objLogFile.WriteLine(Now() & ": Removed all printers from: " & ucase(sServerName)) end sub sub SetPrinterDefault(sPrinterUNC) on error resume next if not bolManagePrinters then if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Default printer to " & sPrinterUNC & _ " skipped because disabled (" & strUser & " on " & strComputerName & ")") else objNetwork.SetDefaultPrinter sPrinterUNC If Err.Number = 0 Then if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Set as Default: " & sPrinterUNC) else if bolWriteLog then objLogFile.WriteLine(Now() & ": Failed: Set as Default: " & sPrinterUNC) end if end if on error goto 0 end sub Sub PutEnv(sName, sValue) Dim oEnv Set oEnv = objShell.Environment("USER") oEnv(sName) = sValue if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Set user variable """ & sName & """ to: """ & sValue & """") Set oEnv = Nothing End Sub Sub Run(ByVal sFile) on error resume next objShell.Run Chr(34) & sFile & Chr(34), 1, false If Err.Number = 0 Then if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Execute: " & sFile) else if bolWriteLog then objLogFile.WriteLine(Now() & ": Failed: Execute: " & sFile) end if on error goto 0 End Sub Sub FileCopy(sSource, sTarget) on error resume next objFileSystem.CopyFile sSource, sTarget If Err.Number = 0 Then if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Copy: " & sSource & " to " & sTarget) else if bolWriteLog then objLogFile.WriteLine(Now() & ": Failed: Copy: " & sSource & " to " & sTarget) end if on error goto 0 End Sub Function isThisIP(sThisAddress) on error resume next dim sThisComputer, oWMIService, cItems, oItem ,sAddress sThisComputer = "." Set oWMIService = GetObject("winmgmts:\\" & sThisComputer & "\root\cimv2") Set cItems = oWMIService.ExecQuery ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True") For Each oItem in cItems 'cycle all adapters 'objItem.MACAddress 'this is the mac address, just in case For Each sAddress in oItem.IPAddress 'cycle all ip addresses for this adapter if sAddress = sThisAddress then isThisIP = true if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Found IP " & sThisAddress ) exit function end if Next Next If Err.Number = 0 Then if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Did not find IP " & sThisAddress ) else if bolWriteLog then objLogFile.WriteLine(Now() & ": Failed: An error occurred looking for IP " & sThisAddress ) end if isThisIP = false On Error goto 0 End Function