PowerShell Ezine, Logon Scripts

Guy's Scripting Ezine 3 - Map Network Drive

Contents of Guy's Scripting Ezine 3 - Map Network Drive

N.B. See a better more up-to-date version of this page Here.

 

1) A secret
2) Script to connect a network drive
3) Logon Script Tip
4) Links to more Scripts
5) Guy's Litmus Tests

1) A secret

This week I was operating outside my usual area of expertise; I needed a CGI (Common Gateway Interface) script for an autoresponder project.  Now Guy knows very little about CGI scripts and this week’s secret is the CGI code made me feel like a beginner. I wished that there were more remarks or comments to help me understand the purpose of the script I was adapting.

So, I have taken to heart this problem of beginners needing explanations in scripts, and this week I have put in lots of comments to explain what each section is about.

ˆ

2) Script to connect a network drive.

There are many learning points in this script
a) Simple mapping network drive method - MapNetworkDrive
b) The logic which disconnects K:\ drive if it is already connected. People often ask me how to check or disconnect a network drive.
c) For, next loops
d) If, then, else statements

Instructions:
Copy the lines following 'Script starts up to 'Script ends.
Paste the script into notepad.
Make changes to the value \\alan\home
Remember to save with a .vbs extension.
Double click to test the script
If you like it, then adapt it and assign it to your users
 

 

'Script starts here.
' VBScript by Guy Thomas
'Purpose of this script is two fold:
'Firstly, it maps the K:\ drive to \\server\share
'Secondly, if the K:\ drive is connected it disconnects it!
' The next line creates the Shell or environment for the commands - do not alter

Set WshShell = WScript.CreateObject("WScript.Shell")

' The section sets the variables. Note the pattern variable = value
' You may change the variable names if you wish to check your understanding.

Set GuyNet = WScript.CreateObject("WScript.Network")
Set CheckDrive = GuyNet.EnumNetworkDrives()

' Be very careful with the DriveLetter, the value must be a CAPITAL letter

DriveLetter = "K:"

' You must change the VALUE \\alan\home, unless you have a server called alan

RemotePath = "\\alan\home"

' This section deals with a For ... Next loop

AlreadyConnected = False
For i = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(i) = DriveLetter Then AlreadyConnected = True
Next

' This section uses the If = then, else logic
' This section tests to see if the Drive is already mapped,
' and if yes then to disconnect
' WshShell.PopUp is not strictly necessary


If AlreadyConnected = True then
GuyNet.RemoveNetworkDrive DriveLetter
GuyNet.MapNetworkDrive DriveLetter, RemotePath
WshShell.PopUp "Drive " & DriveLetter & "Disconnect, then connect successfully"

Else
GuyNet.MapNetworkDrive DriveLetter, RemotePath
WshShell.PopUp "Drive " & DriveLetter & " connected successfully."

End if
'Script ends here
 

 

ˇ

3) Logon script tip

These week’s tip is to place comments in YOUR scripts. In particular, put remarks on the PURPOSE of the script, and the role of each section. The HOW the script is done should be apparent from the variables and the methods.

4) Links to scripts at Guy's site.

1. Map home drive http://computerperformance.co.uk/Logon/Logon_HomeDir.htm
2. Remove network drive http://computerperformance.co.uk/Logon/Logon_Remove.htm

5) Litmus tests

Each Litmus test is fun, yet has a serious learning point. I am sure that there will be at least one test that will suit you. This week's litmus test is:
Professionals put comments on the PURPOSE of the script
Amateurs do not use comments in their scripts.

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

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

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

Please report a broken link, or an error.