Guy's Scripting Ezine 51 - Logon .... Sleep
Contents for Guy's Scripting Ezine 51 - Logon.... Sleep
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
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.
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.
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 intCounter < 10
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
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