All Forums >> [Scripting] >> Post a VBScript >> Basic Logon Script Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
In appreciation for the info and help I recieved here I'm posing my new logon script with the hopes it will help someone else.
It isn't perfect, I'm new to this, and based on some of the postings I've seen; it is pretty basic as well....
'********************************************************************************* 'Main scripting file for my sites 'Written by Bartman 'I'm using this script at 3 separate location. I've changed the contents of each to 'match each site specifically. 'I chose to use Option Explicit to ensure all the variables were correct. I like that. 'Most of this has been created from scripts that I've found and copied. Usually because 'I was able to figure out what they were doing. I'm not a wizard at this stuff. You'll 'notice that I use the asteric lines a lot, it makes is easier for me to read. 'I still need to figure out some things but this is working really well for me. 'Mucho Thanx to all the people who administer and moderate this forum. 'I have edited this file as per the instructions in the Read First postie. '********************************************************************************* Option Explicit ' Force explicit declarations Dim Network 'Network object Dim FSO 'Filesystem object Dim User ' Current user object Dim Domain ' Current User's domain object Dim GrpDict ' Dictionary of groups to which the user belongs Dim Shell ' Set Network = WScript.CreateObject("WScript.Network")'Create the network Set FSO = CreateObject("Scripting.FileSystemObject")'Create the filesystem object Set shell = Wscript.CreateObject("WScript.Shell") Call Cleanup 'I'm going this route till I can get a better handle on my new networks. ' Wait until the user is completely logged in... ' While/Wend loop to ensure the user is logged in completely User = "" While User = "" WScript.Sleep 100 ' 1/10 th of a second User = Network.UserName Wend Domain = Network.UserDomain 'Put the user's account "Member Of" info into a dictionary object. Set GrpDict = CreateMemberOfObject(Domain, User)'create a dictionary of group objects '*************************************************************************************** 'Map Network Drives........... '*************************************************************************************** 'Checks for group membership then maps the appropriate drive(s) 'I'm not controlling the Users drive with this script, that is still done in AD If MemberOf(GrpDict, "admin") Then 'Member of the admin offices group Network.MapNetworkDrive "N:", "\\SERVER\SHARE" Network.MapNetworkDrive "P:", "\\SERVER\SHARE" Network.MapNetworkDrive "S:", "\\SERVER\SHARE" Network.MapNetworkDrive "M:", "\\SERVER\SHARE" Network.MapNetworkDrive "I:", "\\SERVER\SHARE" Else 'Map park's drives for everybody else Network.MapNetworkDrive "N:", "\\SERVER\SHARE" Network.MapNetworkDrive "P:", "\\SERVER\SHARE" Network.MapNetworkDrive "S:", "\\SERVER\SHARE" Network.MapNetworkDrive "M:", "\\SERVER\SHARE" End If '*************************************************************************************** 'End of Drive Mapping script........... '*************************************************************************************** '*************************************************************************************** 'Map Network Printer........... '*************************************************************************************** 'Checks for group membership then maps the appropriate printer(s) 'I'm thinking this should be expanded to the divisional groups printers If MemberOf(GrpDict, "admin") Then 'Member of the admin offices group Network.AddWindowsPrinterConnection "\\SERVER\SHARE" Network.AddWindowsPrinterConnection "\\SERVER\SHARE" Network.SetDefaultPrinter "\\SERVER\SHARE" 'I think this should be done by group Else 'Map park's Printers for everybody else Network.AddWindowsPrinterConnection "\\SERVER\SHARE" Network.AddWindowsPrinterConnection "\\SERVER\SHARE" Network.SetDefaultPrinter "\\SERVER\SHARE" 'I think this should be done by group End If '*************************************************************************************** 'End of Printer Mapping script........... '*************************************************************************************** '*************************************************************************************** 'Changing registry keys........... '*************************************************************************************** 'Use this to make the users homepage the Intranet site. 'Create a variable to hold the registry key you want to change. 'Current User Key Const CU_HP = "HKCU\Software\Microsoft\Internet Explorer\Main\" 'Local Machine Key Const LM_HP = "HKLM\Software\Microsoft\Internet Explorer\Main\" On Error Resume Next 'Current User Key, Default Page URL and Start Page. Shell.RegWrite CU_HP & "Default_Page_URL", "http://SERVER" shell.RegWrite CU_HP & "Start Page", "http://SERVER" 'Local Machine Key, Default Page URL and Start Page. Shell.RegWrite LM_HP & "Default_Page_URL", "http://SERVER" shell.RegWrite LM_HP & "Start Page", "http://SERVER" '*************************************************************************************** 'End of registry keys script........... '*************************************************************************************** '*************************************************************************************** 'Functions for determining group membership........... '*************************************************************************************** Function MemberOf(ObjDict, strKey)'Test for group membership ' Given a Dictionary object containing groups to which the user ' is a member of and a group name, then returns True if the group ' is in the Dictionary otherwise it returns False. ' Inputs: ' strDict - Input, Name of a Dictionary object (IE: GrpDict) ' strKey - Input, Value being searched for in the Dictionary object (IE: Domain Admins) ' Sample Usage: MemberOf = CBool(GrpDict.Exists(strKey))'Boolean variable End Function Function CreateMemberOfObject(strDomain, User) ' Given a domain name and username, returns a Dictionary ' object of groups to which the user is a member of. ' Inputs: ' strDomain - Input, NT Domain name ' User - Input, NT username Dim objUser, objGroup Set CreateMemberOfObject = CreateObject("Scripting.Dictionary") CreateMemberOfObject.CompareMode = vbTextCompare Set objUser = GetObject("WinNT://" _ & strDomain & "/" _ & User & ",user") For Each objGroup In objUser.Groups CreateMemberOfObject.Add objGroup.Name, "-" 'wscript.echo "Is a member of " & objGroup.Name Next Set objUser = Nothing 'Clear the variable End Function '*************************************************************************************** 'End of group membership functions........... '*************************************************************************************** '*************************************************************************************** 'Clear Drive Mappings........... '*************************************************************************************** Function Cleanup On Error Resume Next network.RemoveNetworkDrive "I:" network.RemoveNetworkDrive "J:" network.RemoveNetworkDrive "K:" network.RemoveNetworkDrive "L:" network.RemoveNetworkDrive "M:" network.RemoveNetworkDrive "N:" network.RemoveNetworkDrive "O:" network.RemoveNetworkDrive "P:" network.RemoveNetworkDrive "Q:" network.RemoveNetworkDrive "R:" network.RemoveNetworkDrive "S:" network.RemoveNetworkDrive "T:" network.RemoveNetworkDrive "U:" network.RemoveNetworkDrive "V:" network.RemoveNetworkDrive "W:" network.RemoveNetworkDrive "X:" network.RemoveNetworkDrive "Y:" network.RemoveNetworkDrive "Z:" End Function '*************************************************************************************** 'End of Clear Mappings........... '*************************************************************************************** '*************************************************************************************** 'Send a Message to Users........... '*************************************************************************************** 'I am actually calling a secondary script from this one. It resides on the same drive. 'Shell.run("\\SERVER\SHARE\Message.vbs") '*************************************************************************************** 'End of send message script........... '*************************************************************************************** '*************************************************************************************** 'Clear variables sub........... '*************************************************************************************** Set Network = Nothing Set FSO = Nothing Set User = Nothing Set Domain = Nothing Set GrpDict = Nothing Set Shell = Nothing '*************************************************************************************** 'End of Clearing variables........... '***************************************************************************************
I have some comments/suggestions if you don't mind. They are in no means intended as criticisms, just sharing my opinions.
1) Very well commented. 2) Using block indentation helps make the code easier to read. 3) You should pass objects and variables into functions and subs rather than depending on them being globally created. This makes your code more reuseable. To make it even more reuseable, have the sub/ function do as much as it can by itself. For instance, in your Cleanup sub, have it create its own WSHNetwork object. That way you can simply copy and paste the sub into any other script where you want to run it without having to remember that for the sub to work you need to globally create a WSHNetwork object that has to be named network. 4) It is mostly semantics, but if a function does not return a value (as in your Cleanup function) then there is no need for it to be a function and you can declare it as a sub. This also removes the need to use the Call keyword when you want to execute the code in the sub/function. 5) Personally I prefer to use variable names that give some indication of what the variable is/holds. For instance, instead of a variable named network, I would use a name like oWSNet so I know that the variable refers to an object and that it more specifically is a WScript.Network object. 6) Anytime that you type the same thing more than once, you should examine to see if there is a more automated way to do it. For instance, you typed network.RemoveNetworkDrive 18 times in the Cleanup function. This could be automated with a loop 7) If you plan on expanding the MemberOf function to do other things than it currently does, then ignore this comment. I don't see any advantage to having a function that executes one line of code. Just do the line of code wherever you call the function. I realize that this sort of conflicts with comment 6, but in this case, there really isn't any benefit to be gained from encapsulating one line of code in a function. 8) It is purely personal preference, but I find it is easier to read the code if constants are always declared at the same spot (most people choose the top of the script) and if they are always written in all caps (so you know just by looking at them that they are constants (which I applaud you for doing).
Here is the code with the changes that I talked about. Feel free to completely ignore this entire post if you would like. I will point out that I made the changes without running the code, so it is entirely possible that I introduced some errors. If that is the case, let me know and I will edit what I posted.
I thank you for your comments, especially the praises.
I am small time compared to most of what I'm seeing here. I'm just trying to learn something useful and make my job easier, especially since it just tripled in load.
I've been looking for a way to decide what to map based on IP address, I found one for gateways and that should work. I just need to go through it and see how it will work for me.
Again, Thanx for the comments and advise, don't be surprised if you see it put to use.