Jump to content

Getting the CD-Drive-Letter in PE2


Recommended Posts

Hello,

I write a script for autodeploying images from a dvd. But I need to get the driveletter from the dvd-drive in PE.

Is there a way like a systemvariable to get this letter or a way to detect via vbscript. I have tried this but without success:


function suche_profilordner(nutze_konf)
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
for i = a to z
wscript.echo " Suche DVD-Laufwerk bei Buchstaben " & i & "."
If objFso.FolderExists(i & ":\imagex\" & nutze_konf) = true Then
letter = i
end if
next
if i <> "" then
suche_profilordner = i
else
suche_profilordner = "Fehler:Buchstabe nicht gefunden"
end if

end function

Link to post
Share on other sites

I just use a batch file and diskpart to change the drive letter during my startup process because it was using a letter I wanted my hard drive partitions to use, but you can use the Win32_CDROMDrive.Drive instance as well.

Batch to change the CD drive letter

echo list volume > X:\ListCD.txt
FOR /F "tokens=2,4" %%i IN ('diskpart /s X:\ListCD.txt') DO @IF /I %%j == CD_ROM SET CDROMVOL=%%i
IF DEFINED CDROMVOL echo select volume %CDROMVOL% > X:\ChangeCD.txt
IF DEFINED CDROMVOL echo assign letter=K: >> X:\ChangeCD.txt
IF DEFINED CDROMVOL diskpart /s X:\ChangeCD.txt

VBScript to find CD drive letters

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colCD = objWMIService.ExecQuery("Select * from Win32_CDROMDrive")

For Each objCD In colCD ' If there are multiple CD drives, this loop will only set the variable for the last one listed
strCDLetter = objCD.Drive
Next

Link to post
Share on other sites
  • 2 weeks later...

This won't fit perfectly for you as-is, but this is part of the script I use for CD stuff. I set the cdrom drive with the cdname "WINPE" to G:

If the drive is ready and is a cdrom drive, has a volumename and that name is "WINPE" then

{if the drive is not G: but G: exists, then move G: via diskpart to another letter}

create a text file selecting the drive via drive letter and assign it to be G:, via diskpart.


Set oFileSystem = New Scripting.FileSystemObject

For Each vDrive In oFileSystem.Drives
If vDrive.IsReady Then
If vDrive.DriveType = 4 Then
If LenB(vDrive.VolumeName) <> 0 And vDrive.VolumeName = "WINPE" Then
If vDrive.DriveLetter <> "G" Then
If FolderExists("G:\") Then
ShellAndWait "DISKPART /S X:\WINDOWS\SYSTEM32\NU2MENU\REMOVEG.TXT", vbHide
End If
If (Not FolderExists("G:\")) And LenB(vDrive.DriveLetter) <> 0 Then
Dim oFileWrite As TextStream

Set oFileWrite = oFileSystem.CreateTextFile("X:\WINDOWS\TEMP\FIXCD.TXT", True)

oFileWrite.WriteLine "SELECT VOLUME " & vDrive.DriveLetter
oFileWrite.WriteLine "ASSIGN LETTER=G"
oFileWrite.Close

Set oFileWrite = Nothing

ShellAndWait "DISKPART /S X:\WINDOWS\TEMP\FIXCD.TXT", vbHide
End If
End If
End If
End If

End If
Next

Link to post
Share on other sites

I guess I'll throw in my two cents. I have also written a script to detect the cd-rom drive letter. The main difference from the other two is that I had to account for external drives that are attached via USB to some of our systems. The problem I found is that when WinPE loads, most of the time those drives are not detected right away. There is usually a lag in the mounting time of the drive. So my script addresses that problem by using a timer (timer.vbs) and a loop to look for the drive again. Works perfectly on all my systems.

Dim taskCmd, i, objShell, objFso, collSettings, objItem, cdrom, lword
Set objShell = CreateObject("WScript.Shell")
Set objFso = CreateObject("Scripting.FileSystemObject")

'determine cd-rom drive letter
For i = 1 To 7
taskCmd = "%comspec% /c diskpart /s X:\Windows\system32\probeDrives.txt"
objShell.Run taskCmd,0,true
Set collSettings = objFso.Drives
For Each objItem in collSettings
If objItem.DriveType = 4 Then
lword = objItem.DriveLetter
If objFso.FileExists(lword & ":\sources\boot.wim") Then
cdrom = lword & ":"
Exit For
End If
End If
Next
If cdrom <> "" Or i = 7 Then Exit For
taskCmd = "%comspec% /c cscript X:\Windows\system32\timer.vbs"
objShell.Run taskCmd,0,true
Next

'check whether cdrom was set
If cdrom = "" Then
taskCmd = "Could not determine CD-ROM drive letter"
MsgBox taskCmd,16,"Error"
End If

probeDrives:

list volume
exit

timer.vbs

Dim exittime, x
x = 0
exittime = timer + 8
do while timer < exittime
x = x + 1
loop

Edited by zorphnog
Link to post
Share on other sites
  • 2 weeks later...
I just use a batch file and diskpart to change the drive letter during my startup process because it was using a letter I wanted my hard drive partitions to use, but you can use the Win32_CDROMDrive.Drive instance as well.

Batch to change the CD drive letter

echo list volume > X:\ListCD.txt
FOR /F "tokens=2,4" %%i IN ('diskpart /s X:\ListCD.txt') DO @IF /I %%j == CD_ROM SET CDROMVOL=%%i
IF DEFINED CDROMVOL echo select volume %CDROMVOL% > X:\ChangeCD.txt
IF DEFINED CDROMVOL echo assign letter=K: >> X:\ChangeCD.txt
IF DEFINED CDROMVOL diskpart /s X:\ChangeCD.txt

VBScript to find CD drive letters

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colCD = objWMIService.ExecQuery("Select * from Win32_CDROMDrive")

For Each objCD In colCD ' If there are multiple CD drives, this loop will only set the variable for the last one listed
strCDLetter = objCD.Drive
Next

Wondering just how I'd call this or where to implement it - that "echo list volume" has to run under DISKPART, doesn't it?

Link to post
Share on other sites

I have a need to change the CD-ROM letter to accommodate an image we are deploying that uses letters C, D, and E.

We also have network shares to pull images from, and those are assigned J, K, L, and M.

There are 3 other shares for tools and log reports - I, T, and W.

So I need something to relocate the CD-ROM drive on a system (and there could be 2 of them) - to other drive letters.

I tried your setup and while it ran, it couldn't determine any CD-ROM drive letter.

Then I tried WreX's setup, but was unsure where and how exactly the pieces would be executed.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...