djones (6th March 2008)
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):
Now...printer assignment works and so too does drive mapping. However, neither the 'RemoveNetworkDrives' nor 'RenameNetworkDrives' functions work.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
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; 7th March 2008 at 12:10 AM.
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:
Could this then be shortened to: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
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
Had a quick look and heres your problem:
Your missing 'Next' ie:Code:If NetDrives.Count <> 0 Then For i = 0 To NetDrives.Count - 1 WSHNetwork.RemoveNetworkDrive NetDrives(i) End If
I also noted, you defined WSHNetwork twice.. in you main and removeprintersCode:If NetDrives.Count <> 0 Then For i = 0 To NetDrives.Count - 1 WSHNetwork.RemoveNetworkDrive NetDrives(i) Next End If
djones (6th March 2008)
@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.
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.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
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.
Have you tried without the '\' ?
Just to see if for some reason it's escaping the next character?
Yeah, I thought of that one. No change with the error message AFAIR.
Ok that is odd... cant really see anything wrong with the code... if you post the whole thing, I'll have a look..
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.
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.
Had a look and your problem is that you tried to define mdrive# in the same line i.e:
You need to have a new line for each:Code:mDrive2 = "J:\" mDrive3 = "P:\" mDrive4 = "R:\"
Code:mDrive2 = "J:\" mDrive3 = "P:\" mDrive4 = "R:\"
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:
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.Code:OPTION EXPLICIT ON ERROR RESUME NEXT
djones (13th March 2008)
There are currently 1 users browsing this thread. (0 members and 1 guests)