Discussion:
Create a AD Logonscript which maps multiple network drives on variable drive letters
(too old to reply)
8***@gmail.com
2014-04-29 08:59:31 UTC
Permalink
i found a working script which adds ONE drive this way, but i'm not able to modify it, to work with more than one
could you please help me modify it?
i asked the creator (GUY from www.computerperformance.co.uk) but he said you could aid me better...

many thanks!

code:

' FirstLetter.vbs Windows Logon Script
' VBScript to map the first available drive letter
' Author Guy Thomas http://computerperformance.co.uk/
' Version 3.7 - September 2010
' ------------------------------------------------------'
Option Explicit
Dim strDriveLetter, strRemotePath
Dim objNetwork, objShell
Dim CheckDrive, DriveExists, intDrive
Dim strAlpha, strExtract, intAlpha, intCount

' The section sets the variables
' N.B. Change strRemotePath
strRemotePath = "\\grand\Home"
strDriveLetter = "B:"
strAlpha = "BJMRU"
intAlpha = 0
intCount = 0
err.number= vbEmpty

' This sections creates two objects:
' objShell and objNetwork and then counts the drives
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set CheckDrive = objNetwork.EnumNetworkDrives()

' This section operates the For ... Next loop
' See how it compares the enumerated drive letters
' With strDriveLetter
On Error Resume Next
DriveExists = False
' Sets the Outer loop to check for 5 letters in strAlpha
For intCount = 1 To 5
DriveExists = False

' CheckDrive compares each Enumerated network drive
' with the proposed drive letter held by strDriveLetter
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) = strDriveLetter _
Then DriveExists = True
Next
intAlpha = intAlpha + 1

' Logic section if strDriveLetter does not = DriveExist
' Then go ahead and map the drive

Wscript.Echo strDriveLetter & " exists: " & DriveExists
If DriveExists = False Then objNetwork.MapNetworkDrive _
strDriveLetter, strRemotePath
call ShowExplorer ' Extra code to take you to the mapped drive
' Appends a colon to drive letter. 1 means number of letters
strDriveLetter = Mid(strAlpha, intAlpha,1) & ":"

' If the DriveExists, then it is necessary to
' reset the variable from true --> false for next test loop
If DriveExists = True Then DriveExists = False

Next

WScript.Echo "Out of drive letters. Last letter " & strDriveLetter

WScript.Quit(1)

Sub ShowExplorer()
If DriveExists = False Then objShell.run _
("Explorer" & " " & strDriveLetter & "\" )
If DriveExists = False Then WScript.Quit(0)
End Sub

' Guy's First Available Letter Script Example ends here
R.Wieser
2014-04-29 12:53:11 UTC
Permalink
Hello Kete(?)

As far as I can see the code you posted is supposed to give all remote
drives letters as named in the 'strAlpha' variable, so not "adding" is
needed.

There are two reasons why it could fail to do its job:

1) You try to assign a drive to a letter which is already in use.

2) The "If DriveExists = False Then WScript.Quit(0) " line in the
"ShowExplorer" subroutine aborts the drive-checking prematurily

That means that for 1) the (badly named) "strAlpha" variable may only
contain drive letters that are not yet in use !

Suggestion: add some "debugging" code to the script, so you can see
intermediate results. Like showing the arguments to, as well as the result
of the "objNetwork.MapNetworkDrive" call. It should show you if the script
really tries to assign all networked drives, and if such assignment actually
succeedes.

Hope that helps.
Rudy Wieser

P.s.
I hope you do realize that although network drives might all get
driveletters, they might get different ones every time the script is run.
Not what I would call a usable result (other than for testing)
Post by 8***@gmail.com
i found a working script which adds ONE drive this way, but i'm not able
to modify it, to work with more than one
Post by 8***@gmail.com
could you please help me modify it?
i asked the creator (GUY from www.computerperformance.co.uk) but he said
you could aid me better...
Post by 8***@gmail.com
many thanks!
' FirstLetter.vbs Windows Logon Script
' VBScript to map the first available drive letter
' Author Guy Thomas http://computerperformance.co.uk/
' Version 3.7 - September 2010
' ------------------------------------------------------'
Option Explicit
Dim strDriveLetter, strRemotePath
Dim objNetwork, objShell
Dim CheckDrive, DriveExists, intDrive
Dim strAlpha, strExtract, intAlpha, intCount
' The section sets the variables
' N.B. Change strRemotePath
strRemotePath = "\\grand\Home"
strDriveLetter = "B:"
strAlpha = "BJMRU"
intAlpha = 0
intCount = 0
err.number= vbEmpty
' objShell and objNetwork and then counts the drives
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set CheckDrive = objNetwork.EnumNetworkDrives()
' This section operates the For ... Next loop
' See how it compares the enumerated drive letters
' With strDriveLetter
On Error Resume Next
DriveExists = False
' Sets the Outer loop to check for 5 letters in strAlpha
For intCount = 1 To 5
DriveExists = False
' CheckDrive compares each Enumerated network drive
' with the proposed drive letter held by strDriveLetter
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) = strDriveLetter _
Then DriveExists = True
Next
intAlpha = intAlpha + 1
' Logic section if strDriveLetter does not = DriveExist
' Then go ahead and map the drive
Wscript.Echo strDriveLetter & " exists: " & DriveExists
If DriveExists = False Then objNetwork.MapNetworkDrive _
strDriveLetter, strRemotePath
call ShowExplorer ' Extra code to take you to the mapped drive
' Appends a colon to drive letter. 1 means number of letters
strDriveLetter = Mid(strAlpha, intAlpha,1) & ":"
' If the DriveExists, then it is necessary to
' reset the variable from true --> false for next test loop
If DriveExists = True Then DriveExists = False
Next
WScript.Echo "Out of drive letters. Last letter " & strDriveLetter
WScript.Quit(1)
Sub ShowExplorer()
If DriveExists = False Then objShell.run _
("Explorer" & " " & strDriveLetter & "\" )
If DriveExists = False Then WScript.Quit(0)
End Sub
' Guy's First Available Letter Script Example ends here
8***@gmail.com
2014-04-30 11:24:55 UTC
Permalink
Post by 8***@gmail.com
i found a working script which adds ONE drive this way, but i'm not able to modify it, to work with more than one
could you please help me modify it?
i asked the creator (GUY from www.computerperformance.co.uk) but he said you could aid me better...
many thanks!
' FirstLetter.vbs Windows Logon Script
' VBScript to map the first available drive letter
' Author Guy Thomas http://computerperformance.co.uk/
' Version 3.7 - September 2010
' ------------------------------------------------------'
Option Explicit
Dim strDriveLetter, strRemotePath
Dim objNetwork, objShell
Dim CheckDrive, DriveExists, intDrive
Dim strAlpha, strExtract, intAlpha, intCount
' The section sets the variables
' N.B. Change strRemotePath
strRemotePath = "\\grand\Home"
strDriveLetter = "B:"
strAlpha = "BJMRU"
intAlpha = 0
intCount = 0
err.number= vbEmpty
' objShell and objNetwork and then counts the drives
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set CheckDrive = objNetwork.EnumNetworkDrives()
' This section operates the For ... Next loop
' See how it compares the enumerated drive letters
' With strDriveLetter
On Error Resume Next
DriveExists = False
' Sets the Outer loop to check for 5 letters in strAlpha
For intCount = 1 To 5
DriveExists = False
' CheckDrive compares each Enumerated network drive
' with the proposed drive letter held by strDriveLetter
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) = strDriveLetter _
Then DriveExists = True
Next
intAlpha = intAlpha + 1
' Logic section if strDriveLetter does not = DriveExist
' Then go ahead and map the drive
Wscript.Echo strDriveLetter & " exists: " & DriveExists
If DriveExists = False Then objNetwork.MapNetworkDrive _
strDriveLetter, strRemotePath
call ShowExplorer ' Extra code to take you to the mapped drive
' Appends a colon to drive letter. 1 means number of letters
strDriveLetter = Mid(strAlpha, intAlpha,1) & ":"
' If the DriveExists, then it is necessary to
' reset the variable from true --> false for next test loop
If DriveExists = True Then DriveExists = False
Next
WScript.Echo "Out of drive letters. Last letter " & strDriveLetter
WScript.Quit(1)
Sub ShowExplorer()
If DriveExists = False Then objShell.run _
("Explorer" & " " & strDriveLetter & "\" )
If DriveExists = False Then WScript.Quit(0)
End Sub
' Guy's First Available Letter Script Example ends here
hmm i haven't thought about your point at "p.s." that possibly is a big problem, i have to test it further befor trying it in the live-system

Thank you very much for your suggestion! I'll try and experiment with it.
R.Wieser
2014-04-30 12:01:32 UTC
Permalink
Hey Kete,
Post by 8***@gmail.com
Thank you very much for your suggestion! I'll try and experiment with it.
Let us know what the result is ?

If you can't get it to work its possible that with some more info (like from
those tests) we could pin-point the actual cause.

Also, if you only want to assign fixed drive letters to certain remote
drives than most of the code you have is superfluous. The only thing you
than actually need is the 'objNetwork' object and the 'MapNetworkDrive' call
(providing it a drive letter and the path to the remote drive ofcourse).

By the way: maybe you do not know, but you can use the "path to the remote
drive" directly, instead of a driveletter. Something like the below would be
fully valid:

(in a CMD or alike window)

dir \\grand\Home\path\*.exe

Hope that helps.
Rudy Wieser
Post by 8***@gmail.com
Post by 8***@gmail.com
i found a working script which adds ONE drive this way, but i'm not able
to modify it, to work with more than one
Post by 8***@gmail.com
Post by 8***@gmail.com
could you please help me modify it?
i asked the creator (GUY from www.computerperformance.co.uk) but he said
you could aid me better...
Post by 8***@gmail.com
Post by 8***@gmail.com
many thanks!
' FirstLetter.vbs Windows Logon Script
' VBScript to map the first available drive letter
' Author Guy Thomas http://computerperformance.co.uk/
' Version 3.7 - September 2010
' ------------------------------------------------------'
Option Explicit
Dim strDriveLetter, strRemotePath
Dim objNetwork, objShell
Dim CheckDrive, DriveExists, intDrive
Dim strAlpha, strExtract, intAlpha, intCount
' The section sets the variables
' N.B. Change strRemotePath
strRemotePath = "\\grand\Home"
strDriveLetter = "B:"
strAlpha = "BJMRU"
intAlpha = 0
intCount = 0
err.number= vbEmpty
' objShell and objNetwork and then counts the drives
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set CheckDrive = objNetwork.EnumNetworkDrives()
' This section operates the For ... Next loop
' See how it compares the enumerated drive letters
' With strDriveLetter
On Error Resume Next
DriveExists = False
' Sets the Outer loop to check for 5 letters in strAlpha
For intCount = 1 To 5
DriveExists = False
' CheckDrive compares each Enumerated network drive
' with the proposed drive letter held by strDriveLetter
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) = strDriveLetter _
Then DriveExists = True
Next
intAlpha = intAlpha + 1
' Logic section if strDriveLetter does not = DriveExist
' Then go ahead and map the drive
Wscript.Echo strDriveLetter & " exists: " & DriveExists
If DriveExists = False Then objNetwork.MapNetworkDrive _
strDriveLetter, strRemotePath
call ShowExplorer ' Extra code to take you to the mapped drive
' Appends a colon to drive letter. 1 means number of letters
strDriveLetter = Mid(strAlpha, intAlpha,1) & ":"
' If the DriveExists, then it is necessary to
' reset the variable from true --> false for next test loop
If DriveExists = True Then DriveExists = False
Next
WScript.Echo "Out of drive letters. Last letter " & strDriveLetter
WScript.Quit(1)
Sub ShowExplorer()
If DriveExists = False Then objShell.run _
("Explorer" & " " & strDriveLetter & "\" )
If DriveExists = False Then WScript.Quit(0)
End Sub
' Guy's First Available Letter Script Example ends here
hmm i haven't thought about your point at "p.s." that possibly is a big
problem, i have to test it further befor trying it in the live-system
Post by 8***@gmail.com
Thank you very much for your suggestion! I'll try and experiment with it.
Loading...