Solved

Single Diskpart script with multiple diskpart commands

Posted on 2011-03-14
4
2,598 Views
Last Modified: 2012-05-11
Hello Everyone,

I found  the following batch script that runs diskpart on a Win 2k3 SP2 machine and looks for specific volume labels then returns the volume number that I use later in the script to assign drive letters to those volume numbers.

I have found a way to consistently output the data below to text and parse the text file for the variables I need  - the problem then becomes that I use the variables to then run diskpart again - of course at this point, the volume numbers change and the variables that I just created are no longer valid.

I would like to know if there is a way to run all of this so that I am only running a single diskpart command to keep the volume data from changing.

Here is the script (sorry - it's ugly):

set SCRIPTLOC=d:\scripts

echo Assigning Drive Letters
:: Create a script file to be used by the for loops
      echo list volume > %SCRIPTLOC%\Listvols.tmp

:: Parse the output from 'Diskpart> list volume' for drives
diskpart /s %SCRIPTLOC%\Listvols.tmp > %SCRIPTLOC%\diskpart.tmp 2>&1

      echo Checking for volume SysDB...
      FOR /F "tokens=1-4" %%a IN ('type %SCRIPTLOC%\diskpart.tmp') DO @IF /I "%%c" == "SysDB" @SET XDrive=%%b
      
      echo Checking for volume Quorum...
      FOR /F "tokens=1-4" %%a IN ('type %SCRIPTLOC%\diskpart.tmp') DO @IF /I "%%c" == "Quorum" @SET MDrive=%%b

      echo Checking for volume DbBLogs...
      FOR /F "tokens=1-4" %%a IN ('type %SCRIPTLOC%\diskpart.tmp') DO @IF "%%c" == "DbBLogs" @SET WDrive=%%b

      echo Checking for volume DbB...
      FOR /F "tokens=1-4" %%a IN ('type %SCRIPTLOC%\diskpart.tmp') DO @IF "%%c" == "DbB" @SET LDrive=%%b

      echo Checking for volume DbALogs...
      FOR /F "tokens=1-4" %%a IN ('type %SCRIPTLOC%\diskpart.tmp') DO @IF /I "%%c" == "DbALogs" @SET RDrive=%%b

:: Create a script file to be used by Diskpart
      echo. > %SCRIPTLOC%\ChangeDrive.tmp

      IF DEFINED XDrive echo select volume %XDrive% >> %SCRIPTLOC%\ChangeDrive.tmp
      IF DEFINED XDrive echo assign letter=X: >> %SCRIPTLOC%\ChangeDrive.tmp

      IF DEFINED MDrive echo select volume %MDrive% >> %SCRIPTLOC%\ChangeDrive.tmp
      IF DEFINED MDrive echo assign letter=M: >> %SCRIPTLOC%\ChangeDrive.tmp

      IF DEFINED WDrive echo select volume %WDrive% >> %SCRIPTLOC%\ChangeDrive.tmp
      IF DEFINED WDrive echo assign letter=W: >> %SCRIPTLOC%\ChangeDrive.tmp
      
      IF DEFINED LDrive echo select volume %LDrive% >> %SCRIPTLOC%\ChangeDrive.tmp
      IF DEFINED LDrive echo assign letter=L: >> %SCRIPTLOC%\ChangeDrive.tmp

      IF DEFINED RDrive echo select volume %RDrive% >> %SCRIPTLOC%\ChangeDrive.tmp
      IF DEFINED RDrive echo assign letter=R: >> %SCRIPTLOC%\ChangeDrive.tmp
      
:: Run diskpart using the new script file
      diskpart /s %SCRIPTLOC%\ChangeDrive.tmp

Thanks in advance!!!!
0
Comment
Question by:james_higgs
  • 3
4 Comments
 
LVL 21

Expert Comment

by:AmazingTech
ID: 35216280
If I understand you correctly the volume numbers change when you run your script and all volumes don't get the correct drive letter?

You should use multiple diskpart to get an accurate data just before changing the drive letter.

Give my adjusted script a try.
@echo off
set SCRIPTLOC=d:\scripts

set DriveAndLabel=X:SysDB M:Quorum W:DbBLogs L:DbB R:DbALogs

echo Assigning Drive Letters
:: Create a script file to be used by the for loops
echo list volume > %SCRIPTLOC%\Listvols.tmp


for %%y in (%DriveAndLabel%) do (
    for /F "tokens=1,2 delims=:" %%a in ('echo %%y') do (
        echo Checking for label %%b to assign letter %%a:
        :: Parse the output from 'Diskpart> list volume' for specific volume
        FOR /F "tokens=1-4" %%c IN ('diskpart /s %SCRIPTLOC%\Listvols.tmp ^| find /i " %%b "') do (
            if /i "%%a" == "%%e" (
                ECHO %%b is already assigned to %%a:
            ) else (
                ECHO Assigning %%b to %%a:
           
                echo. > %SCRIPTLOC%\ChangeDrive.tmp

                echo select volume %%d >> %SCRIPTLOC%\ChangeDrive.tmp
                echo assign letter=%%a: >> %SCRIPTLOC%\ChangeDrive.tmp

                :: Run diskpart using the new script file for current volume we're looking for
                diskpart /s %SCRIPTLOC%\ChangeDrive.tmp
            )
        )
    )
)

Open in new window

0
 

Accepted Solution

by:
james_higgs earned 0 total points
ID: 35216462
Thanks very much for the reply AmazingTech...

I think the problem will be that it is running diskpart more than once so there is no guarantee that the disks associated with the volumes will remain constant (I remove drive letters in a previous step, so drive letters won't be associated prior - my apologies that I didn't mention) - so when you run the first diskpart command, it may parse volume with label Quorum as disk 3 - when the second diskpart command is run to assign disk 3 to the drive letter M, it is possible that now disk 3 is actually DbB (I hope this makes sense).  This behavior can be seen by opening diskpart, running the list volume command, taking note of the details, close window and then re-run diskpart again.

I have actually figured out a workaround - I open diskpart from the script (start diskpart)  before running my commands  - what this does is keeps the disk/volume information static for any subsequent diskpart commands - then when I'm done running all my diskpart commands, I use taskkill to close the original diskpart window.  Not pretty, but it works.

Thanks again for the effort and reply!!!!!!
0
 

Author Comment

by:james_higgs
ID: 35216503
Since there is a working workaround, I don't want anyone to spend any more time trying to resolve
0
 

Author Closing Comment

by:james_higgs
ID: 35252764
solution does not answer question but is a workaround
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now