PowerShell Ezine, Logon Scripts

Guy's Scripting Ezine 60 - Renaming a mapped drive

Contents for Ezine 60 - Renaming a mapped drive

This week’s secret

Have you ever bought something and thought: ' I should have got that years ago? '  I had that feeling last year when I switched from ISDN to broadband.  This week I have had that same feeling about a PDF compiler for my ebooks, I should have done this a long time ago.  So thank you Mike G for recommending  Win2PDF

Getting the PDF compiler and adding printer friendly pages is also a triumph for seeking and getting feedback from you the ebook readers.  Left to my own devices, I would have had no idea that you wanted not only printer friendly pages, but also that you preferred PDF format.

ˆ

This week's mission - to rename a mapped drive

There is a story behind this week's script.  For months readers have been asking me how to control the name of a mapped network drive.  At the back of my mind I knew it must be possible to manipulate this drive letter namespace.  Then out of the blue, Barry kindly wrote in and showed me how VBScript can rename the local drive letter.  I simply love readers who send me script ideas.  I thank you again Barry.

Example 1 - To rename the local mapped network drive.

When you map a network drive, the operating system automatically calculates an appropriate name for the label which you see in explorer.  The formula that XP uses is: sharename & computer description name (Letter).  For example: Home on Green Server (z:)   Network managers want to gain control over the namespace of this local drive.  The fact that administrators can right click then Rename this mapped share is a clue that it must be possible to rename the offending object with a VBScript.  Our script will turn this wish into reality.

Instructions

  1. Copy and paste the script below into notepad.
  2. Save the file with .vbs extension e.g NameDrive.vbs
  3. Change the server \\ alan to a server on your network.  Similarly make sure that you have a share called \home, else change the UNC path in the script.
  4. Double click and then open explorer and check the drive name.

 

 

' NameDrive.vbs
' VBScript to map a network drive.
' Authors Guy Thomas and Barry Maybury
' Version 1.3 - January 23rd 2005
' ----------------------------------------'
'
Option Explicit
Dim objNetwork, strDrive, objShell, objUNC
Dim strRemotePath, strDriveLetter, strNewName
'
strDriveLetter = "Y:"
strRemotePath = "\\alan\home"
strNewName = "GuyDives"

' Section to map the network drive
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath

' Section which actually (re)names the Mapped Drive
Set objShell = CreateObject("Shell.Application")
objShell.NameSpace(strDriveLetter).Self.Name = strNewName

Wscript.Echo "Check : "& strDriveLetter & " for " & strNewName
WScript.Quit

' End of script.
 

Learning Points

Note 1:  Observe how we create a shell object in addition to the usual network object.

Note 2: The key command is: objShell.NameSpace(strDriveLetter).Self.Name.  Let us dissect this statement. NameSpace(), means: ' go and get the name of what ever is inside the brackets ', in our case strDriveLetter.  The next feature almost speaks for itself .Self.Name; meaning get the name attribute.  Finally we set the value of strDriveLetter = strNewName.


If you are looking for handy network utilities, try some of the free downloads at Tools4Ever


Example 2 - Adding error correcting code

Anticipation is a marvellous skill.  My error correcting code attempts to think ahead - what could go wrong?  Perfection would be that the script automatically  corrected the problem.  More usually, my code provides a message explaining why the script is in distress and what you can do to solve the problem.  Possible problems are that the UNC path is incorrect or unavailable or that the drive has already been mapped.

Error section A checks to establish that the UNC path is available.  What the script does is mimic you clicking on the Start (menu) and Run, and then typing the UNC path.  If explorer launches at the mapped path, then the script continues mapping the drive, else the script branches and echoes an appropriate error message.

Error section B checks for an error when you try and rename the mapped drive letter.

Error section C deals with situation where the mapped network drive does not exist and so cannot be renamed.

 

' NameDrive.vbs
' VBScript to map a network drive.
' Authors Guy Thomas and Barry Maybury
' Version 2.4 - January 23rd 2005
' ----------------------------------------'
'
Option Explicit
Dim objNetwork, strDrive, objShell, objUNC
Dim strRemotePath, strDriveLetter, strNewName
'
strDriveLetter = "Y:"
strRemotePath = "\\alana\home"
strNewName = "GuyDrive"

On Error Resume Next
' Error section A
' Tries to connect to UNC path
Set objUNC = CreateObject("WScript.Shell")
objUNC.Run strRemotePath
WSCript.Sleep 1500

If err.number = vbEmpty Then
' Closes explorer and creates, MapNetworkDrive
objUNC.SendKeys "%{F4}"
Wscript.Sleep 500
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath

' Error section B
' Relays error if UNC path is incorrect
ElseIf err.number = -2147023693 Then
Wscript.Echo " Check UNC path : " & strRemotePath
WScript.Quit(0)
Else Wscript.Echo "New error number " & err.number
WScript.Quit(1)
End If

' Clear error else error number carries over and script fails
Err.clear

' Section which actually (re)names the Mapped Drive
Set objShell = CreateObject("Shell.Application")
objShell.NameSpace(strDriveLetter).Self.Name = strNewName

' Error section C
If err.number = vbEmpty Then
Wscript.Echo "Check : " & strDriveLetter & " for " & strNewName
ElseIf err.number = 424 Then
WScript.Echo "Problem mapping " & strDriveLetter
WScript.Quit(3)
Else
WScript.echo "New problem " & err.number
WScript.Quit(4)
End IF
WScript.Quit
' End of script.

Learning Points

Note 1: The script creates a shell object and then ' Runs ' the UNC path.  If this is successful then the script cleverly closes the explorer and then goes ahead and maps the network drive.

 

Note 2: If you run the script with:  WScript.Echo err.number  then you can trap the -2147xxxx error decimal value.  Then you can use this number as the basis of an If then.... ElseIf construction.  Naturally to obtain the error value you would have to deliberately alter the strRemotePath to a non-existent server.

 

Note 3:  Err.clear is a tiny command but it is essential in cases where the first part of the script fails, yet the second part could actually work.  One scenario is that you have already mapped the strDriveLetter and even thought the server is down, you still want to try and rename the drive letter.

If the above scenario is true, then remove the WScript.quit(x) lines.

ˇ

Summary - Renaming a mapped network drive

Perhaps you are irritated by the name that the operating assigns a mapped network drive?  If so, then here is the script to rename that long description of the mapped drive to text of your choosing.

See more on Mapping Network Drives - Whole Section here

Computer Training Software - Recommended Training VideosGuy Thomas recommends Computer Training Software

Their topics and material are ideal for getting you started with VBScript.  The videos are easy to follow and you can control the pace.  Try their free demo material and then see if you want to buy the full package. See more about VB Script Training CD.


 *


Google

WebComputerperformance.co.uk

GFi Events Manager

Guy Recommends: GFi EventsManager

Here is a solution to monitor, manage and archive thousands of events that are generated by devices across your entire network.  Get your free evaluation copy of GFI EventsManager.

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.