A while back I was presented with the following problem during a Operating System Deployment Project. If a computer had a multi-card reader it would assign itself to the following 7 drives:
- E:\
- F:\
- G:\
- H:\
- I:\
- J:\
- K:\
This wasn’t so much a problem, except that the Drives H:\, I:\, J:\ & K:\ were used for Network locations and as a result those drives did not map via the logon script. So I had to move the Multi-Card reader off those drives.
It was decided that we would move the whole lot and not just the ones that were conflicting as this would enable us to create a standard deployment strategy.
So I wrote a script that would move all removable drives off the lower level drive letters and move them up the alphabet to a section that wasn’t quite so populated.
I then added this script to a SCCM Package and deployed it through the Task Sequence. When the machine was built the Multi-Card reader would start at “O:\” and not “E:\”.
Simple solution, works very well. Hopefully someone else will find some value in this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
'========================================================================== ' ' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 2012 ' ' NAME: SetRemovableDriveLetter.vbs ' ' AUTHOR: Chisholm, Chris - Long View Systems ' DATE : 07/17/2012 ' ' COMMENT: Set's Removable Media to available drive letters starting with DRIVE2START ' '========================================================================== Option Explicit Const ForReading = 1 Const ForWriting = 2 Const ForAppending = 8 Const DRIVE2START = "O" 'Defines drive to start populating at. Dim CurrentDriveANSI : CurrentDriveANSI = Asc(DRIVE2START) Dim objNetwork : Set objNetwork = CreateObject("WScript.Network") Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject") Dim objShell : Set objShell = CreateObject("WScript.Shell") Dim strComputer : strComputer = objNetwork.ComputerName Dim TempPath : TempPath = objShell.ExpandEnvironmentStrings("%TEMP%") Call Main WScript.Quit Sub Main Call SetRemovableDrives End Sub Sub SetRemovableDrives Dim objWMIService, colItems, objItem, colVolumes, objVolume Dim intLoopCounter : intLoopCounter = 0 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk WHERE DriveType = '2'") For Each objItem in colItems 'Get Removable Drive Volumes Set colVolumes = objWMIService.ExecQuery("Select * from Win32_Volume Where DriveLetter = '" & objItem.Name & "'") For Each objVolume In colVolumes If Asc(Mid(objVolume.DriveLetter,1,1)) < CurrentDriveANSI Then WriteLog "Need to relocate drive " & objVolume.DriveLetter 'Loop while finding an open drive letter Do While intLoopCounter < 1 'This will never exit on it's own. If CurrentDriveANSI > Asc("Z") Then WriteLog "All Drives allocated, failed to move drive" Exit Do 'Failsafe exit in the event that all drives are taken End If If Not DriveExists(Chr(CurrentDriveANSI)) Then WriteLog "Relocating drive " & objVolume.DriveLetter & " to " & Chr(CurrentDriveANSI) & ":" objVolume.DriveLetter = Chr(CurrentDriveANSI) & ":" objVolume.Put_ CurrentDriveANSI = CurrentDriveANSI + 1 'Increase the drive letter as this one is now used. Exit Do End If Loop End If Next Next End Sub Function DriveExists(strDriveLetter) WriteLog "Checking for existing drive " & strDriveLetter Dim objWMIService, colVolumes, objVolume Dim bFoundDrive : bFoundDrive = False Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colVolumes = objWMIService.ExecQuery("SELECT * FROM Win32_Volume WHERE DriveLetter = '" & strDriveLetter & ":'") For Each objVolume in colVolumes bFoundDrive = True CurrentDriveANSI = CurrentDriveANSI + 1 'Increase the drive letter as this one is used. Next If bFoundDrive Then WriteLog "Drive " & strDriveLetter & " is NOT avaliable for use." Else WriteLog "Drive " & strDriveLetter & " is avaliable for use." End If DriveExists = bFoundDrive End Function Sub WriteLog(Text) Dim objWriteLog : Set objWriteLog = objFSO.OpenTextFile(TempPath & "\SetRemovableDrive.log", ForAppending, True) objWriteLog.Write Time & vbTab & Text & VbCrLf objWriteLog.Close End Sub |
March 7, 2014 at 8:21 am
Hi Chris,
Your script looks very good, excelent JOB.
Could You help me With the runing the script ?
I have tried to use/run but script ended with error about line 53. When I try to comment this line script doesn’t finish with error but writed to Log file >
15:56:32 Need to relocate drive D:
15:56:32 All Drives allocated, failed to move drive
15:56:32 Need to relocate drive E:
15:56:32 All Drives allocated, failed to move drive
15:56:32 Need to relocate drive F:
15:56:32 All Drives allocated, failed to move drive
15:56:32 Need to relocate drive G:
15:56:32 All Drives allocated, failed to move drive
I’m not a specialist about vbs code 🙁 please help
Walda
March 10, 2014 at 7:58 am
For some reason it thinks all your drive letters are taken. What drive are you trying to start from?