|
Guy recommends :
Find out which of your VMs are a waste of space and which VMs need more resources. |
Windows Logon Script - To Map the First Available Drive LetterIntroduction to Mapping the First Available Drive LetterThis page builds on the simple MapNetworkDrive script. Consequently, I advise you to take a refresher on my Map Net Drv Simple script, before attempting this example. The key point of the advanced script on this page is that it checks to see if the drive is already available. Should the letter be in use, then VBScript tries the next letter in sequence, which you can define. Normally, if you execute a MapNetworkDrive script for a second time, the script would fail. However, this script has error-correcting logic and if the drive is already mapped, then it tries an alternative letter. You could view this script as a bit of fun, or as a chance to experiment with additional scripting techniques such as EnumNetworkDrives, looping and if .. then ... else.. endif commands. Topics for Mapping the First Available Drive Letter
♦ Mission - To Map to the First Available Drive LetterHave you ever experienced the problem where your script attempts to map a network drive, but unfortunately the drive letter is already in use? Well my script overcomes this 'in use' error. What the code does is enumerate the mapped drives, as a result it can detect drive letters that are already in use. Not only does it avoid an error, but it also loops to the next letter in your sequence. One possible scenario is that you want to map a network drive, but you do not really mind which letter the script chooses. Therefore, you provide a list of alternative letters and the script maps the first available drive letter. Scripts constantly remind me that they merely automate what we humans can do manually. Where scripts are at their best, is when you have a repetitive sequence of tasks. The trick is to choose the best type of loop for the job, for example For...Next, or Do... Until. The trap is omitting essential commands inside the loop, or else leave vital sequences outside the loop. What makes this script complicated, is that it has not one loop, but two loops, one inside the other. Example 1 - To Map to the First Available Drive LetterThe question my script keeps asking is this: 'Does this particular drive letter exist?' If the answer is false, then the drive letter is available, so code choose to map that particular letter. Job done. However, if that particular letter is already in use, then the loop tests the next letter in the sequence and repeats my DriveExists test. Instead of my normal tactic of building the script in stages, I have decided to present you with the full script in one complete example. One reason for this unusual approach is that it's difficult to separate the two loops. As there are no preparation scripts on this page I strongly recommend you review both the MapNetworkDrive and especially, the EnumNetworkDrives scripts. Instructions for mapping to the first available drive letter
' FirstLetter.vbs Windows Logon Script Learning Points for Mapping to the First Available Drive LetterNote 1: Trace the outer loop, focussing on strDriveLetter. For a given letter, ask, 'Does this letter = any of the existing mapped network drives?' If yes, then loop and get another letter from strAlpha. If no then success. The script maps that letter because it is free and there is no danger of a conflict. Note 2: The inner loop is where the script compares strDriveLetter with each drive exposed by the EnumNetworkDrives function. Note 3: You may prefer to choose a different selection of drive letters for strAlpha. Surprisingly, the list does not have to be in alphabetical order. However, you should not include the letters of local hard drives. Note 4: Observe the importance of EnumNetworkDrives, and check the plural spelling of this method. Note 5: The subroutine ShowExplorer() at the end of the script is not strictly necessary, it merely opens the explorer to confirm that the mapped drive now exists. Challenges : If you take up my theme of developing your own scripts; experiment with different variable names, amend my WScript.Echo messages, add error correcting code, choose different letters to map. Guy Recommends Solarwinds' Free Network Monitor![]() Thus utility makes it easy to check the health of your router or firewall. Check the real-time performance, and availability statistics, for any device on your network. Get started with an extensive collection of "out-of-the-box" monitors for popular network devices. Download your free Network Device Monitor Example 2 - Variation which does not launch Windows Explorer' FirstLetter.vbs Windows Logon Script Summary of Mapping the First Available Drive LetterIt is annoying if a map network drive script fails simply because the drive has already been mapped. This script loops through a sequence of letters until it finds a free drive letter. Note how it employs only MapNetworkDrive, but also EnumNetworkDrives. Download my Logon Script eBook for only $6.25
If you like this page then please share it with your friends
See more logon scripts examples• Logon Script Home • Vbscript Map Network Drive Username • Multiple Mapped Network Drives • Map Network Drive Script • Disconnect Network Drives • Map Network Drive Group • ObjNetwork.MapNetworkDrive • Logon script group policy • EnumNetworkDrives *
|
||||