+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
Scripts Thread, Logon Script Help in Coding and Web Development; Please bare with me on this one! I am trying to consolidate a few functions etc into one script for ...
  1. #1

    Join Date
    Oct 2007
    Location
    Cambridgeshire, UK
    Posts
    306
    Thank Post
    58
    Thanked 23 Times in 20 Posts
    Rep Power
    17

    Logon Script Help

    Please bare with me on this one!

    I am trying to consolidate a few functions etc into one script for all that assigns printers (according to computer name and hence location), maps drives (according to group membership), etc...

    I used ScriptBuilder to build the initial script and am now trying to add a few functions from other scripts that I use/have used. However, my scripting skills leave at lot to be desired and I am struggling to get various bits working.

    Here is what I have so far (minus some repetitive bits):

    Code:
    ' Set Environment Variables
    
    OPTION EXPLICIT
    ON ERROR RESUME NEXT
    DIM WSHNetwork, objFSO
    DIM ComputerName, UserName
    SET WSHNetwork = CreateObject("Wscript.Network")
    SET objFSO = CreateObject("Scripting.FileSystemObject")
    ComputerName = WSHNetwork.ComputerName
    UserName = UCASE(WSHNetwork.UserName)
    
    ' Clean Up Any Previous Settings
    
    RemoveNetworkPrinters
    RemoveNetworkDrives
    
    ' Wait For Disconnect
    
    wscript.sleep 300
    
    ' Add Network Printers By Computer Name
    
    IF BeginsWith(ComputerName,"AMBER") THEN
    	AddPrinter "\\server\AmberBW", True
    	AddPrinter "\\server\AmberColour", False
    END IF
    IF BeginsWith(ComputerName,"CURLAP") THEN
    	AddPrinter "\\server\IoliteBW", False
    	AddPrinter "\\server\JadeBW", False
    	AddPrinter "\\server\AmethystColour", False
    	AddPrinter "\\server\MoonstoneBW", False
    	AddPrinter "\\server\MoonstoneColour", False
    	AddPrinter "\\server\AmberBW", False
    	AddPrinter "\\server\JasperColour", False
    	AddPrinter "\\server\JadeColour", False
    	AddPrinter "\\server\JasperBW", False
    	AddPrinter "\\server\EmeraldColour", False
    	AddPrinter "\\server\AmberColour", False
    	AddPrinter "\\server\ReceptionColour", False
    	AddPrinter "\\server\EmeraldBW", False
    	AddPrinter "\\server\ReceptionBW", False
    	AddPrinter "\\server\IoliteColour", False
    	AddPrinter "\\server\AmethystBW", False
    END IF
    
    ' Removed: Lots of additional printer assignments by rooms/areas
    
    ' Map Network Drives By User Group
    
    IF IsMember("ANCILLARY STAFF") THEN
    	MapDrive "G:", "\\server\Staff Share$"
    END IF
    IF IsMember("CLASS TEACHERS") THEN
    	MapDrive "J:", "\\server\Curriculum$"
    	MapDrive "G:", "\\server\Staff Share$"
    	MapDrive "R:", "\\server\Pupil Resources$"
    	MapDrive "P:", "\\server\Pupils$"
    END IF
    IF IsMember("OFFICE STAFF") THEN
    	MapDrive "O:", "\\server\Office Share$"
    	MapDrive "G:", "\\server\Staff Share$"
    	MapDrive "S:", "\\server\Adapps$"
    END IF
    IF IsMember("PUPILS") THEN
    	MapDrive "R:", "\\server\Pupil Resources$"
    	MapDrive "P:", "\\server\Pupils$"
    END IF
    IF IsMember("SLT") THEN
    	MapDrive "O:", "\\server\Office Share$"
    	MapDrive "J:", "\\server\Curriculum$"
    	MapDrive "P:", "\\server\Pupils$"
    	MapDrive "R:", "\\server\Pupil Resources$"
    	MapDrive "S:", "\\server\Adapps$"
    	MapDrive "G:", "\\server\Staff Share$"
    END IF
    IF IsMember("TAS") THEN
    	MapDrive "J:", "\\server\Curriculum$"
    	MapDrive "P:", "\\server\Pupils$"
    	MapDrive "G:", "\\server\Staff Share$"
    	MapDrive "R:", "\\server\Pupil Resources$"
    END IF
    
    ' Add Further Settings
    
    RenameNetworkDrives
    BGInfo
    StartMenuClick
    CopyMoveToFolder
    
    ' Sub Routine Code
    
    sub RemoveNetworkPrinters
    
    	Dim i
    	Dim objPrinters,WSHNetwork
    	Dim PrinterPath 
    
    	Set WSHNetwork = WScript.CreateObject("WScript.Network") 
    	Set objPrinters = WSHNetwork.EnumPrinterConnections 
    
    	for i = 0 to objPrinters.Count - 1 
    		
    		PrinterPath = objPrinters.Item(i) 
    		' * Only remove network printers
    		if mid(PrinterPath, 1, 2) = "\\" then 
    			WSHNetwork.RemovePrinterConnection PrinterPath, True, True 
    		end if
            
    	next 
    
    end sub
    
    sub RemoveNetworkDrives
    
    	Dim NetDrives
    	Dim i
    
    	Set NetDrives = WSHNetwork.EnumNetworkDrives
    
    	If NetDrives.Count <> 0 Then
    		For i = 0 To NetDrives.Count - 1
    		WSHNetwork.RemoveNetworkDrive NetDrives(i)
    	End If
    
    end sub
    
    sub RenameNetworkDrives
    
    	mDrive1 = "G:\"
    	mDrive2 = "J:\"
    	mDrive3 = "O:\"
    	mDrive4 = "P:\"
    	mDrive5 = "R:\"
    	mDrive6 = "S:\"
    
    	Set oShell = CreateObject("Shell.Application")
    		oShell.NameSpace(mDrive1).Self.Name = "School Organisation"
    		oShell.NameSpace(mDrive2).Self.Name = "Curriculum Planning"
    		oShell.NameSpace(mDrive3).Self.Name = "Office"
    		oShell.NameSpace(mDrive4).Self.Name = "Pupil Folders"
    		oShell.NameSpace(mDrive5).Self.Name = "Pupil Resources"
    		oShell.NameSpace(mDrive6).Self.Name = "MIS"
    
    end sub
    
    sub BGInfo
    
    	Set objShell = CreateObject("Wscript.Shell")
    	
    	objShell.Run "\\server\NETLOGON\BGInfo\BGInfo.exe \\server\NETLOGON\BGInfo\Default.bgi /nolicprompt /timer:0", 1, True
    
    end sub
    
    sub StartMenuClick
    
    	Dim smpath
    	
    	smpath = "HKCU\Control Panel\Desktop\"
    	
    		'The following line will REQUIRE a click in the start menu
    
    	WSHShell.RegWrite smpath & "MenuShowDelay","65535","REG_SZ"
    
    		'To undo what the above line has done, 
    		'comment out the above line and uncomment the following...
    
    	'WSHShell.RegWrite smpath & "MenuShowDelay","400","REG_SZ"
    
    end sub
    
    sub CopyMoveToFolder
    
    	Dim ctmtpath
    
    	ctmtpath = "HKEY_CLASSES_ROOT\AllFilesystemObjects\shellex\ContextMenuHandlers\"
    	
    	WSHShell.RegWrite ctmtpath,"CopyTo"
    	WSHShell.RegWrite ctmtpath & "\CopyTo\","{C2FBB630-2971-11D1-A18C-00C04FD75D13}"
    	WSHShell.RegWrite ctmtpath,"MoveTo"
    	WSHShell.RegWrite ctmtpath & "\MoveTo\","{C2FBB631-2971-11d1-A18C-00C04FD75D13}"
    
    end sub
    
    ' Built-in Functions
    
    Function EndsWith(ByVal str1, ByVal str2)
    
    	If Len(str2) > Len(str1) Then
    		EndsWith = False
    	Else
    		If Mid(str1, Len(str1) - Len(str2) + 1, Len(str2)) = str2 Then
    			EndsWith = True
    		Else
    			EndsWith = False
    		End If
    	End If
    
    End Function
    
    Function BeginsWith(ByVal str1, ByVal str2)
    
    	If Mid(str1, 1, Len(str2)) = str2 Then
    		BeginsWith = True
    	Else
    		BeginsWith = False
    	End If
    
    End Function
    
    Function IsMember(Byval groupName)
    
    	DIM domain, blnIsMember
    	DIM objUser, grp
    
    	domain = WSHNetwork.UserDomain
    	blnIsMember = False
    	SET objUser = GetObject("WinNT://" & domain & "/" & UserName & ",user")
    	For Each grp In objUser.Groups
    		If LCASE(grp.Name) = LCASE(groupName) Then
    			blnIsMember = True
    			Exit For
    		End If
    	Next
    	IsMember = blnIsMember
    
    End Function
    
    Function MapDrive(Byval Drive, Byval Path)
    
    	If Not objFSO.DriveExists(Drive) Then
    		WSHNetwork.MapNetworkDrive Drive, Path
    	End If
    
    End Function
    
    Function AddPrinter(Byval PrintServer, Byval Default)
    
    	WSHNetwork.AddWindowsPrinterConnection(PrintServer)
    	IF Default THEN
    		WSHNetwork.SetDefaultPrinter PrintServer
    	End If
    
    End Function
    
    ' Clean Up Variables
    
    set WSHNetwork = Nothing
    Set objFSO = nothing
    Set objUser = nothing
    Set objShell = nothing
    Set oShell = nothing
    Set clDrives = nothing
    Set objPrinters = nothing
    Set smpath = nothing
    
    ' Quit the Script
    
    wscript.quit
    Now...printer assignment works and so too does drive mapping. However, neither the 'RemoveNetworkDrives' nor 'RenameNetworkDrives' functions work.

    A less important couple of bits that also fail are the 'CopyMoveToFolder' and 'StartMenuClick'. I got these snippets from the tech-tips forums and thought they'd be useful additions!

    I appreciate I'm probably trying to run before I walk but I figure that if you don't try, you don't learn and don't succeed!

    Can anyone help spot any errors or suggest improvements to make this work?

    Thanks, Dave.
    Last edited by djones; 6th March 2008 at 11:10 PM.

  2. #2

    Join Date
    Oct 2007
    Location
    Cambridgeshire, UK
    Posts
    306
    Thank Post
    58
    Thanked 23 Times in 20 Posts
    Rep Power
    17
    I just had a thought about the 'RenameNetworkDrives' part...

    Would it work (I can't test at the moment) if the relevant code was included at the end of each 'IF' statement? For example:

    Code:
    ' Map Network Drives By User Group
    
    IF IsMember("ANCILLARY STAFF") THEN
    	MapDrive "G:", "\\server\Staff Share$"
    
                 mDrive1 = "G:\"
                 Set oShell = CreateObject("Shell.Application")
    		oShell.NameSpace(mDrive1).Self.Name = "School Organisation"
    
    END IF
    IF IsMember("CLASS TEACHERS") THEN
    	MapDrive "J:", "\\server\Curriculum$"
    	MapDrive "G:", "\\server\Staff Share$"
    	MapDrive "R:", "\\server\Pupil Resources$"
    	MapDrive "P:", "\\server\Pupils$"
    
                 mDrive1 = "G:\"
    	mDrive2 = "J:\"
    	mDrive4 = "P:\"
    	mDrive5 = "R:\"
    
    	Set oShell = CreateObject("Shell.Application")
    		oShell.NameSpace(mDrive1).Self.Name = "School Organisation"
    		oShell.NameSpace(mDrive2).Self.Name = "Curriculum Planning"
    		oShell.NameSpace(mDrive4).Self.Name = "Pupil Folders"
    		oShell.NameSpace(mDrive5).Self.Name = "Pupil Resources"
    
    END IF
    Could this then be shortened to:

    Code:
    ' Set Environment Variables
    
    Set oShell = CreateObject("Shell.Application")
    
    ' Map Network Drives By User Group
    
    IF IsMember("ANCILLARY STAFF") THEN
    	MapDrive "G:", "\\server\Staff Share$"
    
    	oShell.NameSpace(G:\).Self.Name = "School Organisation"
    
    END IF
    IF IsMember("CLASS TEACHERS") THEN
    	MapDrive "J:", "\\server\Curriculum$"
    	MapDrive "G:", "\\server\Staff Share$"
    	MapDrive "R:", "\\server\Pupil Resources$"
    	MapDrive "P:", "\\server\Pupils$"
    
    	oShell.NameSpace(G:\).Self.Name = "School Organisation"
    	oShell.NameSpace(J:\).Self.Name = "Curriculum Planning"
    	oShell.NameSpace(P:\).Self.Name = "Pupil Folders"
    	oShell.NameSpace(R:\).Self.Name = "Pupil Resources"
    
    END IF

  3. #3
    apeo's Avatar
    Join Date
    Sep 2005
    Location
    Lost
    Posts
    1,612
    Thank Post
    95
    Thanked 115 Times in 111 Posts
    Rep Power
    41
    Had a quick look and heres your problem:

    Code:
    	If NetDrives.Count <> 0 Then
    		For i = 0 To NetDrives.Count - 1
    		WSHNetwork.RemoveNetworkDrive NetDrives(i)
    	End If
    Your missing 'Next' ie:

    Code:
    	If NetDrives.Count <> 0 Then
    		For i = 0 To NetDrives.Count - 1
    		WSHNetwork.RemoveNetworkDrive NetDrives(i)
    Next
    	End If
    I also noted, you defined WSHNetwork twice.. in you main and removeprinters

  4. Thanks to apeo from:

    djones (6th March 2008)

  5. #4

    Join Date
    Oct 2007
    Location
    Cambridgeshire, UK
    Posts
    306
    Thank Post
    58
    Thanked 23 Times in 20 Posts
    Rep Power
    17
    @apeo: Thanks, that did the trick.

    OK, I've now to sort the renaming bit. The code I'm trying to use works in another (old) logon script but not in this one which gives a weird error message.

    Code:
    ' Map Network Drives By User Group
    
    IF IsMember("CLASS TEACHERS") THEN
    	MapDrive "J:", "\\server\Curriculum$"
    	MapDrive "G:", "\\server\Staff Share$"
    	MapDrive "R:", "\\server\Pupil Resources$"
    	MapDrive "P:", "\\server\Pupils$"
    
                 mDrive1 = "G:\"
    	mDrive2 = "J:\"
    	mDrive4 = "P:\"
    	mDrive5 = "R:\"
    
    	Set oShell = CreateObject("Shell.Application")
    	oShell.NameSpace(mDrive1).Self.Name = "School Organisation"
    	oShell.NameSpace(mDrive2).Self.Name = "Curriculum Planning"
    	oShell.NameSpace(mDrive4).Self.Name = "Pupil Folders"
    	oShell.NameSpace(mDrive5).Self.Name = "Pupil Resources"
    
    END IF
    When I try to run it this way, I get an "expected end statement" (or similar) at line # (mDrive2 = "J:\"), character # (the quote mark before the 'J'). However, this error doesn't seem to marry up with anything.

    I have tried a few different combinations/placements but I feel like I'm stumbling in the dark and not really getting anywhere!

    Any suggestions?

    Dave.

  6. #5

    Join Date
    Jul 2007
    Location
    Devon
    Posts
    233
    Thank Post
    8
    Thanked 9 Times in 8 Posts
    Rep Power
    15
    Have you tried without the '\' ?

    Just to see if for some reason it's escaping the next character?

  7. #6

    Join Date
    Oct 2007
    Location
    Cambridgeshire, UK
    Posts
    306
    Thank Post
    58
    Thanked 23 Times in 20 Posts
    Rep Power
    17
    Yeah, I thought of that one. No change with the error message AFAIR.

  8. #7
    apeo's Avatar
    Join Date
    Sep 2005
    Location
    Lost
    Posts
    1,612
    Thank Post
    95
    Thanked 115 Times in 111 Posts
    Rep Power
    41
    Ok that is odd... cant really see anything wrong with the code... if you post the whole thing, I'll have a look..

  9. #8

    Join Date
    Oct 2007
    Location
    Cambridgeshire, UK
    Posts
    306
    Thank Post
    58
    Thanked 23 Times in 20 Posts
    Rep Power
    17
    Quote Originally Posted by apeo View Post
    Ok that is odd... cant really see anything wrong with the code... if you post the whole thing, I'll have a look..
    Thank you, here it is at present...

    Dave.
    Attached Files Attached Files

  10. #9

    Join Date
    Jul 2007
    Location
    Devon
    Posts
    233
    Thank Post
    8
    Thanked 9 Times in 8 Posts
    Rep Power
    15
    What did you write it in?

    When I open the file (notepad) and find that section, there are some extra characters on the lines... (namely a little square box thingy)... not sure if that makes a difference... never created anything in something other than notepad for the simple reason, it has no fomatting skills.

  11. #10

    Join Date
    Oct 2007
    Location
    Cambridgeshire, UK
    Posts
    306
    Thank Post
    58
    Thanked 23 Times in 20 Posts
    Rep Power
    17
    So it has! Editted in EditPad Lite and Notepad depending on which computer I was on at the time. Never noticed any problems before - I've removed the extra characters and tidied the formatting and will try again tomorrow.

    Thanks, Dave.

  12. #11

    Join Date
    Oct 2007
    Location
    Cambridgeshire, UK
    Posts
    306
    Thank Post
    58
    Thanked 23 Times in 20 Posts
    Rep Power
    17
    Quote Originally Posted by apeo View Post
    I also noted, you defined WSHNetwork twice.. in you main and removeprinters
    I wasn't sure whether subroutines needed the variables defining separately. Can I simply specify them once at the start?

  13. #12
    apeo's Avatar
    Join Date
    Sep 2005
    Location
    Lost
    Posts
    1,612
    Thank Post
    95
    Thanked 115 Times in 111 Posts
    Rep Power
    41
    Quote Originally Posted by djones View Post
    I wasn't sure whether subroutines needed the variables defining separately. Can I simply specify them once at the start?
    If you define it in the main code as a public variable the the subroutines can use that variable.

  14. #13
    apeo's Avatar
    Join Date
    Sep 2005
    Location
    Lost
    Posts
    1,612
    Thank Post
    95
    Thanked 115 Times in 111 Posts
    Rep Power
    41
    Had a look and your problem is that you tried to define mdrive# in the same line i.e:

    Code:
    mDrive2 = "J:\"	mDrive3 = "P:\"	mDrive4 = "R:\"
    You need to have a new line for each:

    Code:
    mDrive2 = "J:\"
    mDrive3 = "P:\"
    mDrive4 = "R:\"

  15. #14

    Join Date
    Oct 2007
    Location
    Cambridgeshire, UK
    Posts
    306
    Thank Post
    58
    Thanked 23 Times in 20 Posts
    Rep Power
    17
    Quote Originally Posted by apeo View Post
    Had a look and your problem is that you tried to define mdrive# in the same line i.e:

    Code:
    mDrive2 = "J:\"	mDrive3 = "P:\"	mDrive4 = "R:\"
    You need to have a new line for each:

    Code:
    mDrive2 = "J:\"
    mDrive3 = "P:\"
    mDrive4 = "R:\"
    Yeah, this was a formatting error - it was all supposed to be on separate lines. However, it still hasn't solved the problem - there must be something else I'm missing :-(

    Thanks for the help so far!

    Dave.

  16. #15
    apeo's Avatar
    Join Date
    Sep 2005
    Location
    Lost
    Posts
    1,612
    Thank Post
    95
    Thanked 115 Times in 111 Posts
    Rep Power
    41
    No probs... Ok just wondered, are you running the code exactly as it is? The one attached in the same as your just variable have been defined in each line.

    I only ask cause when im debugging i dont use:

    Code:
    OPTION EXPLICIT
    ON ERROR RESUME NEXT
    If you leave option explicit in then you need to define every variable before you use it so if you run the script in its current format, yes it wont work. Also the 'on error resume next', well thats selfexplanatory.
    Attached Files Attached Files

  17. Thanks to apeo from:

    djones (13th March 2008)

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Pro Set Pre-logon
    By mattx in forum Wireless Networks
    Replies: 11
    Last Post: 15th September 2008, 11:16 AM
  2. LogOn Message
    By karldenton in forum Windows
    Replies: 10
    Last Post: 28th January 2008, 06:17 PM
  3. Logon.bat query
    By speckytecky in forum Scripts
    Replies: 3
    Last Post: 20th April 2007, 10:08 AM
  4. Yet another Logon Script
    By ajbritton in forum Windows
    Replies: 3
    Last Post: 19th June 2006, 09:47 AM
  5. Ctrl+Alt+Del at logon
    By Michael in forum Windows
    Replies: 6
    Last Post: 30th March 2006, 02:33 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •