PowerShell Ezine, Logon Scripts

Guy's Scripting Ezine 87 - Re-visiting MapNetworkDrive

Contents for Ezine 87 - Re-visiting MapNetworkDrive

This week's secret

In my youth, my mother used to say that a woman's work is never done.  If I apply her sentiment to VBScript, I could say that a scriptwriter's code is never done.  When I first wrote the AlreadyConnected logon script back in Ezine 32, I thought that it was perfect.  However, this week when a reader asked for a variation, I suddenly became aware of at least three improvements that I could make to this faithful old script.

My message to those of a nervous disposition is that it's OK to copy other peoples script and make amendments.  My message to the confident and the gung-ho, is that you can always improve upon your script.

The highlight of this week's ezine is my 'Learning points' section where I explain how I have adapted and amended the Already Connected logon script.


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


Scenario: You want to map a network drive.  However, you also want to remove any existing mapped drives.

I start with the assumption that you already familiar with the MapNetworkDrive method.  The situation is that while you want to map a drive letter to specific UNC path, you wish to take the opportunity to clean up any old drive mappings.  Yes, you want to map strDriveLetter to strRemotePath, but you also want to delete any other mapped drive letters.

From a learning point of view, I want to show you how to adapt and improve a basic script (Ezine 32) to a different scenario.

ˆ

Script to Map Network Drive - Also deletes all other drives.

This script started out as a solution to the situation where the drive was already mapped.  The problem was when you ran the original script a second time you got a drive already in use error message.

Prerequisites

This script needs a shared drive.  If all else fails create a shared folder on the machine where you run the script.  If you are not familiar with MapNetworkDrive, then have a look at Ezine 32.

Instructions for Creating a VBScript to display lastLogon time.

  1. Edit this variable: strRemotePath = " \ \grand\home"
  2. Do you want to map to the X: drive? Check strDriveLetter = UCase("x:" ) '
  3. Copy and paste the example script below into notepad or use a VBScript editor.
  4. One advantage of a good script editor such as OnScript is that you can see the line numbers (See menu on the upper left for free copy).
  5. Save the file with a .vbs extension, for example: RemoveThenMap.vbs 
  6. Double click RemoveThenMap.vbs and check the message box.

Example - To Map Network Drive - Plus Remove any Existing Mapped Drives

 

' RemoveThenMap.vbs Windows Logon Script
' VBScript to remove all previous mapped drives before
' using MapNetworkDrive to create the new network drive.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.2 - September 2005
' ------------------------------------------------------'
Option Explicit
Dim objNetwork, objShell, CheckDrive
Dim strDriveLetter, strRemotePath, intDrive, bforce

' The section sets the variables.
strDriveLetter = UCase("x:" ) ' Force UPPER case
strRemotePath = "\\grand\home"
bforce = true

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

' This section deletes all mapped network drives
On Error Resume Next
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) <> strDriveLetter _
Then objNetwork.RemoveNetworkDrive _
CheckDrive.Item(intDrive), bforce
Next

objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
objShell.PopUp "Drive " & strDriveLetter & _
" connected successfully."

WScript.Quit

' Guy's Map Network Drive Script ends here

 

Learning Points

At the risk of repeating my previous overconfidence, I now believe that this script is the bee's knees, the cat's whiskers and 'the business'.  Here are the three features in the script that give me particular satisfaction.

Note 1:  Trace the role of the CheckDrive variable.  See how it relates to the EnumNetworkDrives method.  What I particularly like is the way that the script exposes any mapped drives with .item and item(intDrive).  It is thanks to CheckDrive that I am able to delete any extra drive letters.

Note 2:  As its name suggests, bForce is a Boolean enforce command to disconnect existing drives whether or not they are open or in use.  Observe that there is just one comma in this command:
Then objNetwork.RemoveNetworkDrive CheckDrive.Item(intDrive), bforce

Note 3:  Must strDriveLetter be in uppercase?  I used to think so, but I confess that when I double-checked, this script worked fine with a lowercase drive letter.  Still, it costs little for that nice touch "UCase()", which protects against, a) forgetting to use uppercase and b) some clients are case sensitive (Win98).

Summary of Mapped Network Drive

Scripts are fluid and flexible.  The job of the initial script was to map a network drive.  After modification, the script can delete any existing drives and also overcome the 'drive already in use' error.

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.