Jump to content

Recommended Posts

Posted (edited)

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):

 

' 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.

Edited by djones
Posted

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:

 

' 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:

 

' 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

Posted

Had a quick look and heres your problem:

 

	If NetDrives.Count <> 0 Then
	For i = 0 To NetDrives.Count - 1
	WSHNetwork.RemoveNetworkDrive NetDrives(i)
End If

 

Your missing 'Next' ie:

 

	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

  • Thanks 1
Posted

@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.

 

' 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.

Posted
Ok that is odd... cant really see anything wrong with the code... if you post the whole thing, I'll have a look..
Posted
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.

login3.txt

Posted

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.

Posted

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.

Posted

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?

Posted
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.

Posted

Had a look and your problem is that you tried to define mdrive# in the same line i.e:

 

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

 

You need to have a new line for each:

 

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

Posted
Had a look and your problem is that you tried to define mdrive# in the same line i.e:

 

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

 

You need to have a new line for each:

 

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.

Posted

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:

 

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.

login3.txt

  • Thanks 1
Posted (edited)

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.

 

Turbo! I didn't fully understand what 'option explicit' did. Without that in (I'm sure I tried it before but hey...!) it worked. I also had to add another 'on error resume' to the removedrives sub routine to ensure that all existing drives were removed.

 

sub RemoveNetworkDrives

Dim NetDrives
Dim i

Set WSHNetwork = WScript.CreateObject("WScript.Network") 
Set NetDrives = WSHNetwork.EnumNetworkDrives
On Error Resume Next
If NetDrives.Count <> 0 Then
	For i = 0 To NetDrives.Count - 1
	WSHNetwork.RemoveNetworkDrive NetDrives(i)
Next	
End If

end sub

 

Thanks very much for all your help! I'll upload the complete script for future reference when I've tidied it up a bit.

 

Dave.

Edited by djones

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...