Diskpart error

Today I was troublehooting the automated deployment (Altiris) for Windows 2003 R2 terminal server. A part of the deployment is changing de driveletter for the cdrom drive from D: to Z: and creating a second partition with the drive letter D:.

The problem the customer invided me for is the failing of the job to create the second partition. After an short investigation I determined the failing of the creating of the D: partition is caused by an error in the job that changes the drive letter of the cd-rom drive.

The script ended with a green check mark within Altiris because the script had no errorhandling. So the first step to troubleshoot this problem is to add errorhandling to the script.

@Echo Off

SET ERRORLEVEL = 0
IF NOT EXIST C:\TEMP MD C:\TEMP
SET TEMP = C:\TEMP
REM Define the new drive letter of the CD-Rom
SET NewDrive=Z

REM Lookup the drive letter of the CD-Rom
FOR /F "tokens=2 delims==" %%A IN ('wmic cdrom get drive /format:list^|find "="') DO SET CurrentDrive=%%A

REM Lookup the volume assigned to this driveletter
ECHO LIST VOLUME>"%Temp%\DiskpartListVolume"
FOR /F "tokens=2,3 delims= " %%A IN ('diskpart /s "%Temp%\DiskpartListVolume"^|find "olume"') DO If %%B==%CurrentDrive:~0,1% SET CDRomVolume=%%A
DEL /Q "%Temp%\DiskpartListVolume"
ECHO Volume = %CdRomVolume%

REM Assign the newe driveletter with DISKPART
ECHO SELECT VOLUME %CDRomVolume% >"%TEMP%\DiskPartChangeLetter"
ECHO ASSIGN LETTER=%NewDrive:~0,1%>>"%TEMP%\DiskPartChangeLetter"
CALL DISKPART.EXE /S "%TEMP%\DiskPartChangeLetter">"%TEMP%\DiskPartOutput"
DEL /Q "%TEMP%\DiskPartChangeLetter"
DEL /Q "%TEMP%\DiskPartOutput"

REM Inform user
Echo The drive letter of the CD-Rom drive (volume %CDRomVolume%) has been changed from %CurrentDrive% to %NewDrive%:.

REM Check if the drive letter change of the CD-Rom was succesful
FOR /F "tokens=2 delims==" %%A IN ('wmic cdrom get drive /format:list^|find "="') DO SET CurrentDrive=%%A

IF %NewDrive%: NEQ %CurrentDrive% SET Errorlevel=50001

EXIT %ERRORLEVEL%

At first I make sure that the errorlevel is set back to 0 at the beginning of the script to be sure we don’t adapt the errors from a pervious script. The script runs the tool diskpart twice, the first time to collect which volume is the cdrom drive. The second time is to actually change the drive letter.

With the errorhandling in place the script fails and Altiris displays my custom error 50001.

But now I have to find the cause of the failure.
The first step is to remove

@Echo Off

so I can see the output of the different commands. During this part I notice that the output off the

 Diskpart List Volume

command changes between the first and the second run. HOW IS THIS POSIBLE? My friend Google has the asnwer: KB937252 So the problem is caused by an update for Diskpart. To avoid this problem I add the following to the beginning of the script:

REM To avoid changes in the Volume numbering within Diskpart we launch Diskmgmt.msc
Start Diskmgmt.msc

With Disk Manager loaded the output of the diskpart command does not change.

Was once an enthusiastic PepperByte employee but is now working at Ivanti. His blogs are still valuable to us and we hope to you too.