Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mapped drives using VBScript - What's wrong with this code snippet? 3

Status
Not open for further replies.

Callahan

Technical User
Nov 21, 2001
174
GB
Hi all,

Now first off I know as much about VB Scripting as I've learnt this morning. After the sucess I've had so far (up until this point) I'm really going to learn more about it.

Ok, so here's what I want to do. Upon user logon, the .vbs script will unmap all network drives on the client machine, remap them (this is to automate unmapping and mapping of new network shares in the future), then renames each of the drives.
Everything works up until the point of renaming the network drives. If I run the piece of code that renames the drives outside of this code it works so I'm guessing the mistake can only be minor.

Any suggestions grately received.

Option Explicit
Dim strDriveLetter1, strDriveLetter2, RemotePath1, RemotePath2
Dim objNetwork
Dim bForce, bUpdateProfile
Dim mDrive
bForce = "True"
bUpdateProfile = "True"

Set objNetwork = CreateObject("WScript.Network")
strDriveLetter1 = "F:"
strDriveLetter2 = "Y:"
RemotePath1 = "\\fpsl01\home"
RemotePath2 = "\\fpsl01\jbloggs"

' Removes all listed DriveLetters, with bForce, pUpdate Profile
On Error Resume Next
objNetwork.RemoveNetworkDrive strDriveLetter1, bforce, bUpdateProfile
objNetwork.RemoveNetworkDrive strDriveLetter2, bforce, bUpdateProfile

' Maps the listed drives
objNetwork.MapNetworkDrive strDriveLetter1, RemotePath1
objNetwork.MapNetworkDrive strDriveLetter2, RemotePath2

' Section which actually (re)names the Mapped Drives
Set objShell = CreateObject("Shell.Application")
objShell.NameSpace(strDriveLetter1).Self.Name = "test1"
objShell.NameSpace(strDriveLetter2).Self.Name = "test2"
Wscript.Echo "Success - All done"
Wscript.Quit
 
What happend if you replace this:
bForce = "True"
bUpdateProfile = "True"
with this ?
bForce = True
bUpdateProfile = True

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PHV, the script runs through without any errors now except that it still won't rename the mapped drives. Can't understand why it's ignoring this last part:

Set objShell = CreateObject("Shell.Application")
objShell.NameSpace(strDriveLetter1).Self.Name = "test1"
objShell.NameSpace(strDriveLetter2).Self.Name = "test2
 
Callahan,

The reason it appears to ignore these lines is because only local drives can be renamed.

You may be able to see this error with this code:

Option Explicit
On Error Resume Next

'* Variable declaration
Dim strDriveLetter1:strDriveLetter1 = "F:"
Dim strDriveLetter2:strDriveLetter2 = "Y:"
Dim RemotePath1:RemotePath1 = "\\fpsl01\home"
Dim RemotePath2:RemotePath2 = "\\fpsl01\jbloggs"

'* Object creation
Err.Clear
Dim objNetwork:Set objNetwork = CreateObject("WScript.Network")
If Err.Number Then
WScript.Echo Err.Number & " " & Err.Description
WScript.Quit
End If
Dim objShell:Set objShell = CreateObject("Shell.Application")
If Err.Number Then
WScript.Echo Err.Number & " " & Err.Description
WScript.Quit
End If

' Removes all listed DriveLetters, with bForce, pUpdate Profile
Err.Clear
objNetwork.RemoveNetworkDrive strDriveLetter1, True, True
If Err.Number Then WScript.Echo Err.Number & " " & Err.Description
Err.Clear
objNetwork.RemoveNetworkDrive strDriveLetter2, True, True
If Err.Number Then WScript.Echo Err.Number & " " & Err.Description

' Maps the listed drives
Err.Clear
objNetwork.MapNetworkDrive strDriveLetter1, RemotePath1
If Err.Number Then WScript.Echo Err.Number & " " & Err.Description
Err.Clear
objNetwork.MapNetworkDrive strDriveLetter2, RemotePath2
If Err.Number Then WScript.Echo Err.Number & " " & Err.Description

' Section which actually (re)names the Mapped Drives
Err.Clear
objShell.NameSpace(strDriveLetter1).Self.Name = "test1"
If Err.Number Then WScript.Echo Err.Number & " " & Err.Description
Err.Clear
objShell.NameSpace(strDriveLetter2).Self.Name = "test2"
If Err.Number Then WScript.Echo Err.Number & " " & Err.Description

Wscript.Echo "Script finished.
 
MyITGuy, VBScript can rename ANY drive (local or mapped).

Callahan, try this code snippet. I've noticed that the script will ignore the code if the mapping process has not yet completed.

Code:
WScript.Sleep 300
Set objShell = CreateObject("Shell.Application")
objShell.NameSpace(strDriveLetter1).Self.Name = "test1"
objShell.NameSpace(strDriveLetter2).Self.Name = "test2"

Hope This Helps,

Good Luck!
 
'check that the drive you remove isnt already connected to the right place, if it is then there is no need for the time consuming process of remove/remap
'before remapping check the FSO.FolderExists
'take you data out of your script and put it in a xml,ini,txt/cmdlineparams
 
Thanks everyone for your suggestions. Monsterjta, I tried your suggestion using WScript.Wait 300 but to no avail. I suspected this wouldn't work as I have been trying it while logged onto a test machine with mapped drives. A manual rename works so I'm not letting go of the fact that this muxt be possible in a script! If anyone has any other suggestions which may help, please post them, I'll continue looking for some examples on the web.

Thanks again for all of your help.
 
Don't know what to say, Callahan. I use that EXACT code for my logon mappings. Works like a charm.

Looks like your script may have gone through a couple iterations since the original post. Would you like to re-post it?

Hope This Helps,

Good Luck!
 
Sure.

Option Explicit
Dim strDriveLetter1, strDriveLetter2, RemotePath1, RemotePath2
Dim objNetwork
Dim bForce, bUpdateProfile
Dim mDrive
bForce = True
bUpdateProfile = True

Set objNetwork = CreateObject("WScript.Network")
strDriveLetter1 = "H:"
strDriveLetter2 = "Z:"
RemotePath1 = "\\server1\home"
RemotePath2 = "\\server2\software$"

' Removes all listed DriveLetters, with bForce, pUpdate Profile
On Error Resume Next
objNetwork.RemoveNetworkDrive strDriveLetter1, bforce, bUpdateProfile
objNetwork.RemoveNetworkDrive strDriveLetter2, bforce, bUpdateProfile

' Maps the listed drives
objNetwork.MapNetworkDrive strDriveLetter1, RemotePath1
objNetwork.MapNetworkDrive strDriveLetter2, RemotePath2

' Section which actually (re)names the Mapped Drives
WScript.Sleep 300
Set objShell = CreateObject("Shell.Application")
objShell.NameSpace(strDriveLetter1).Self.Name = "test1"
objShell.NameSpace(strDriveLetter2).Self.Name = "test2"
Wscript.Echo "Success - All done"
Wscript.Quit

I think I should just point out that the script successfully disconnects the drives and remaps them but it will not remap them using a different name. Once you change the name of a drive and then disconnect it and run this script to reconnect it and rename the mapped drive to something else it fails and maps it with the old name. Is the the same for you Monsterjta? I'd strongly recommend that you don't change this on your AD login if you're going to test it though!
 
Just out of curiousity...

What if you extend the sleep period to, say, 3000?

OR,

What if you manually disconnect the mapped drives, comment out the code to remove the mapped drives, and try running the script. I'd like to see what happens if you take the "disconnect" portion out of the script.

Hope This Helps,

Good Luck!
 
Using 3000 makes no difference.

Commenting out the part that removes the mapped drives (leaving me with this)...

Option Explicit
Dim strDriveLetter1, strDriveLetter2, RemotePath1, RemotePath2
Dim objNetwork
Dim bForce, bUpdateProfile
Dim mDrive
bForce = True
bUpdateProfile = True

Set objNetwork = CreateObject("WScript.Network")
strDriveLetter1 = "H:"
strDriveLetter2 = "Z:"
RemotePath1 = "\\server\home"
RemotePath2 = "\\server\software$"

' Removes all listed DriveLetters, with bForce, pUpdate Profile
' On Error Resume Next
' objNetwork.RemoveNetworkDrive strDriveLetter1, bforce, bUpdateProfile
' objNetwork.RemoveNetworkDrive strDriveLetter2, bforce, bUpdateProfile

' Maps the listed drives
objNetwork.MapNetworkDrive strDriveLetter1, RemotePath1
objNetwork.MapNetworkDrive strDriveLetter2, RemotePath2

' Section which actually (re)names the Mapped Drives
WScript.Sleep 3000
Set objShell = CreateObject("Shell.Application")
objShell.NameSpace(strDriveLetter1).Self.Name = "test1"
objShell.NameSpace(strDriveLetter2).Self.Name = "test2"
Wscript.Echo "Success - All done"
Wscript.Quit

causes an error to pop up saying that the variable 'objShell' is undefined on line 26 (the part that renames the mapped drives. This seems odd to be because the variable is set on the line above ( Set objShell = CreateObject("Shell.Application") )

Any ideas?
 
Ah, you have an option explicit and didn't DIM objShell.

Code:
Option Explicit
Dim strDriveLetter1, strDriveLetter2, RemotePath1, RemotePath2
Dim objNetwork, objShell
Dim bForce, bUpdateProfile
Dim mDrive
bForce = True
bUpdateProfile = True

Hope This Helps,

Good Luck!
 
monsterjta, you are the man! Thanks so much. I've now bought a book on VBScripting so will hopefully not be asking any more stupid questions! Will be voting for you this week!

Thanks again.
 
You're welcome.

Hey, whenever you're having issues with a script it's a good idea to comment out the 'on error resume next' line. This line, of course, will ignore any errors within the code.

Also, it's a good idea to use the option explicit...but when you do you MUST define all variables before using them. Without option explicit, it is not required to DIM your variables before use.
 
Callahan,

This is your code with error checking (On Error Resume Next is required for error checking). You should only need to edit the User-supplied Constants section.

Code:
Option Explicit
'* Turn off error checking so that we can evaluate the Error
'* returned.
On Error Resume Next

'* User-supplied Constants
Const strDriveLetter1 = "H:"
Const strDriveLetter2 = "Z:"
Const strDriveName1 = "test1"
Const strDriveName2 = "test2"
Const RemotePath1 = "\\server\home" 
Const RemotePath2 = "\\server\software$"

'* Constants
Const WSH_TYPE_SUCCESS = 0
Const WSH_TYPE_ERROR = 1
Const WSH_TYPE_WARNING = 2
Const WSH_TYPE_INFORMATION = 4
Dim intDuration:intDuration = 0

'* Dimension objects
Err.Clear
'* WScript.Shell is needed for event logging.
Dim objWSHShell:Set objWSHShell = CreateObject("WScript.Shell")
'* If the WScript.Shell object is not created, exit the script.
If Err.Number Then WScript.Quit
'* WScript.Network is needed for drive removal and mapping.
Dim objWSHNetwork:Set objWSHNetwork = CreateObject("WScript.Network")
If Err.Number Then
	objWSHShell.LogEvent WSH_TYPE_ERROR, "Unable to create WScript.Network object." & VbCrLf & VbCrLf & Err.Number & " " & Err.Description
	WScript.Quit
End If
'* Scripting.FileSystemObject is needed for checking for existing drives.
Dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")
If Err.Number Then
	objWSHShell.LogEvent WSH_TYPE_ERROR, "Unable to create Scripting.FileSystemObject object." & VbCrLf & VbCrLf & Err.Number & " " & Err.Description
	WScript.Quit
End If
'* Shell.Application is needed for checking for existing drives.
Dim objShell:Set objShell = CreateObject("Shell.Application")
If Err.Number Then
	objWSHShell.LogEvent WSH_TYPE_WARNING, "Unable to create Shell.Application object." & VbCrLf & VbCrLf & Err.Number & " " & Err.Description
	Err.Clear
End If

'* Remove the drive, map and rename.
If objFSO.DriveExists(strDriveLetter1) = True Then objWSHNetwork.RemoveNetworkDrive strDriveLetter1, True, True
If Err.Number Then
	objWSHShell.LogEvent WSH_TYPE_WARNING, "Unable to remove network drive. (" & strDriveLetter1 & ")" & VbCrLf & VbCrLf & Err.Number & " " & Err.Description
Else
	objWSHNetwork.MapNetworkDrive strDriveLetter1, RemotePath1
	If Err.Number Then
		objWSHShell.LogEvent WSH_TYPE_WARNING, "Unable to map network drive. (" & strDriveLetter1 & ")" & VbCrLf & VbCrLf & Err.Number & " " & Err.Description
	Else
		If IsObject(objShell) Then
			intDuration = 0
			Do Until intDuration = 60
				intDuration = intDuration + 1
				If objFSO.DriveExists(strDriveLetter1) = True Then
					Err.Clear
					objShell.NameSpace(strDriveLetter1 & "\").Self.Name = strDriveName1
					If Err.Number Then
						objWSHShell.LogEvent WSH_TYPE_WARNING, "Network drive mapped, but not renamed. (" & strDriveLetter1 & ")" & VbCrLf & VbCrLf & Err.Number & " " & Err.Description
					Else
						objWSHShell.LogEvent WSH_TYPE_SUCCESS, "Network drive (" & strDriveLetter1 & ") mapped and renamed."
					End If
					Exit Do
				End If
			Loop
		End If
	End If
End If
If objFSO.DriveExists(strDriveLetter2) = True Then objWSHNetwork.RemoveNetworkDrive strDriveLetter2, True, True
If Err.Number Then
	objWSHShell.LogEvent WSH_TYPE_WARNING, "Unable to remove network drive. (" & strDriveLetter2 & ")" & VbCrLf & VbCrLf & Err.Number & " " & Err.Description
Else
	objWSHNetwork.MapNetworkDrive strDriveLetter2, RemotePath2
	If Err.Number Then
		objWSHShell.LogEvent WSH_TYPE_WARNING, "Unable to map network drive. (" & strDriveLetter2 & ")" & VbCrLf & VbCrLf & Err.Number & " " & Err.Description
	Else
		If IsObject(objShell) Then
			intDuration = 0
			Do Until intDuration = 60
				intDuration = intDuration + 1
				If objFSO.DriveExists(strDriveLetter2) = True Then
					Err.Clear
					objShell.NameSpace(strDriveLetter2 & "\").Self.Name = strDriveName2
					If Err.Number Then
						objWSHShell.LogEvent WSH_TYPE_WARNING, "Network drive mapped, but not renamed. (" & strDriveLetter2 & ")" & VbCrLf & VbCrLf & Err.Number & " " & Err.Description
					Else
						objWSHShell.LogEvent WSH_TYPE_SUCCESS, "Network drive (" & strDriveLetter2 & ") mapped and renamed."
					End If
					Exit Do
				End If
			Loop
		End If
	End If
End If

Set objWSHShell = Nothing
Set objWSHNetwork = Nothing
Set objFSO = Nothing
Set objShell = Nothing
 
Thanks MyITGuy,

I can use this for future reference too when learning VBScripting. One final small question in case anyone is still reading, is there a wildcard option in VBScript that I could use? Specifically, to remove all network drives, instead of using seperate lines like this:

objNetwork.RemoveNetworkDrive DriveLetter1, bforce, bUpdateProfile
objNetwork.RemoveNetworkDrive DriveLetter2, bforce, bUpdateProfile

is there perhaps a way to just say "Remove all network drives"?
 
Remove all network drives
Set N = CreateObject("WScript.Network")
Set E = N.EnumNetworkDrives
For i = 0 to E.Count - 1 Step 2
If Trim(E.Item(i) & "") > "" Then
N.RemoveNetworkDrive E.Item(i), True, True
Else
N.RemoveNetworkDrive E.Item(i+1), True, True
End If
Next

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
All works like a charm. I'm currently trying to make the script as much hands off as possible. All users on the network have the same drives mapped which makes things easy. The only variable is the user's home folder. As I don't want to create a seperate logon script for each user and VBScript doesn't accept something like %username% as AD does I am now looking for a way to unmap all drives except the user's H: drive. That way the home folder can be mapped using AD and the script will take care of the rest. I'd never need to touch the script again apart from updating it when new shares are added to the network.
So, what I'm looking into now is a way to add an exclusion to the loop that PHV supplied. "Unmap all the network drives except the H: drive." As I'm still new to the world of VBScript it would be helpful if someone could let me know what the loop should look like so I can implement it now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top