PowerShell Ezine, Logon Scripts

Guy's Scripting Ezine 51 - Logon .... Sleep

Contents for Guy's Scripting Ezine 51 - Logon.... Sleep

This week’s secret

Last month, my postbag has more problems concerning Windows 98 clients than any other topic.  If ever there was an illustration of TCO (Total cost ownership), then it is how much time and money is consumed in supporting Windows 98.  In an extreme case, one techie confided in me that either he, or his mate would be laid off when they migrated to XP. His boss knew there just would not be enough work for both of them.

If I step back and take a top down view, then on the one hand, I can see clearly why everyone should now be using XP, but on the other hand, I do like to help people with their scripting problems - even with Windows 98.

ˆ

This week's mission - to make the script wait

While this week's learning point is the Do While... Loop; the purpose of this week's script is to map a network drive to a username.  Therefore, make sure that you have a server with an appropriate share, for example:

\\ server* \ home \ username*.  *Naturally change these to suit your network

If the user has not yet logged on, then it is not going to be possible to manipulate the username variable.  The answer is to avoid failure and design a check that VBScript has obtained objNetwork.UserName, before it proceeds to map the network drive.  Moreover, if the check fails then we build in a loop so that the script waits in the hope that the slow client or the slow network will eventually allow a logon and thus obtain a value for objNetwork.UserName.

Example 1 Logon .... Sleep  Instructions

  1. Copy and paste the script below into OnScript (or notepad.
  2. Save the file with .vbs extension e.g. SleepLoop.vbs.
  3. Double click the .vbs file and examine the mapped network drives in explorer.

 

 

' SleepLoop.vbs
' Purpose VBScript to map a network drive.
' Learning Points: Loop, Sleep, MapNetworkDrive
' Usage if you have Windows 98 clients who are slow to logon
' Author Guy Thomas corrected by G.E.
' Version 2.3 - January 2005
' --------------------------------------------------------------'
Option Explicit
Dim objNetwork, intCounter
Dim strDrive, strPath, strUser

' Here are your 3 variables
' N.B. pay attention to \\server\share\
' \\server\share\username MUST Exist
strDrive = "X:"
strPath = "\\alan\home\"
intCounter = 1

' Create the network object = objNetwork
Set objNetwork = CreateObject("WScript.Network")

' Get the user name.
' Anticipate that on Windows 98, the user may not be logged on.
strUser = objNetwork.UserName

' Here is the Do While Loop
    Do While strUser = ""
        Wscript.Sleep 2000   ' Two Seconds
        strUser = objNetwork.UserName
    Loop


' Note the concatenation of strUser with ampersand
objNetwork.MapNetworkDrive strDrive, strPath & strUser

WScript.Echo "Drive mapped = " & strDrive

WScript.Quit

'end of script
 

Learning Points

Note 1: Important, please check and amend the variables on lines 15 and 16.

Note 2: This week's feature it the Do While..... Loop.  We want to keep checking to see if the user has logged on.  The test is: does strUser = "" (Null).

Note 3: Experiment with different timings.  With WScript.Sleep, a value of 1000 = 1 second delay.

Note 4: Trace how all the variables come together and map the network drive.

ˇ

Extra Challenge:

Add a second loop.  My goal here is to anticipate that in some cases, the network will never connect.  The second loop acts as a timeout.  Do While intCounter < 10 gives the loop ten chances to obtain a value for strUser.

 

Find this section:
 

' Here is the Do While Loop
    Do While strUser = ""
        Wscript.Sleep 2000 ' Two Seconds
        strUser = objNetwork.UserName
    Loop


 

 

Replace with:

Do While intCounter < 10
' Here is the Do While Loop
       Do While strUser = ""
       Wscript.Sleep 2000 ' Two Seconds
       strUser = objNetwork.UserName
       Loop
   if strUser = "" then
   intCounter = intCounter + 1
   Else
   intCounter = 12
End if
Loop

Learning Points

My idea behind the second loop is to cater for the worst case scenario, situations where the script will never find strUser.  The principle is that each loop increases intCounter and when intCounter reaches 10, it will stop looping.  At that point the script will progress to the following section.  After 10 increments, 20 seconds will have elapsed and strUser should have a value.  However, in cases where the script obtains strUser in the first loop, there will be no delay.

As with most of my scripts, I designed this example myself, so if you can see a better way of achieving the goal, then do write in and share your idea with me.

On the other hand, if you want more advanced tools then check out Tools4Ever

Summary

Old network clients, like older gentlemen, need extra time to get the job done.  So in both cases cut them a little slack, and give them an extra loop.  You never know when a delay loop will be useful, so take the time to understand how the Do While.... Loop statements operate.

 

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.