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.
Guy Recommends: The Free IP Address Tracker (IPAT)
Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets. IPAT cracks this problem of allocating IP addresses in networks in two ways:
For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges.
For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker
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
- Copy and paste the script below into OnScript (or notepad.
- Save the file with .vbs extension e.g. SleepLoop.vbs.
- 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.
See more about VBScript
• VBScripts • Ezines • WMI • Logon Scripts • Tool Kit
• Ezine 26 SendKeys • Ezine 34 Scriptomatic • Ezine 47 .put •Ezine 51 Sleep • Ezine 52 OS
• Ezine 77 Scriptomatic • Ezine 84 AcctInfo • Ezine 88 Progress bar • Ezine 89 SendKeys
• Ezine 97 Net Time • Ezine 98 W32 Time • Ezine 99 Time services • Ezine 120 Sendkeys