Trying to create DISKPART script using batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
A_Bobby
Posts: 38
Joined: 21 Oct 2010 12:48

Trying to create DISKPART script using batch file

#1 Post by A_Bobby » 10 May 2014 07:07

Hi,
I have about 100 servers that will be presented with SAN storage. I want to create a diskpart script that will initialize disk, create volume, format and assign drive letter. Each server will be presented with four LUNs 180 GB each. The problem is that these LUNs are being presented along with some garbage 3 MB LUNs that is interrupting the disk assignment process if I use diskpart script which has constant values. The following is a sample output of list disk command using diskpart from one of the servers.

Disk ### Status Size Free Dyn Gpt
-------- ------------- ------- ------- --- ---
Disk 0 Online 118 GB 0 B
Disk 1 Offline 2880 KB 2880 KB
Disk 2 Offline 2880 KB 2880 KB
Disk 3 Offline 2880 KB 2880 KB
Disk 4 Offline 2880 KB 2880 KB
Disk 5 Offline 180 GB 180 GB
Disk 6 Offline 180 GB 180 GB
Disk 7 Offline 180 GB 180 GB
Disk 8 Offline 180 GB 180 GB

As you can see there are 4 3 MB garbage LUNs. This is not a constant output. On some servers the 180 GB starts as disk 2 or disk 3, so I want to create a script that identifies only the 180 GB LUNs and formats and assigns drive letters as desired. Here's the script I have created so far but it falls short

@echo off
setlocal EnableDelayedExpansion
del *.txt
@echo list disk >listdisk.txt
diskpart /s listdisk.txt >>diskdetail1.txt
for /f "tokens=2" %%a in ('findstr /i /c:"180 GB" diskdetail1.txt') do @echo Disk %%a >>diskdetail2.txt
@echo The following 180 GB disks were found..
type diskdetail2.txt

-------------------------------------------
the output I get in diskdetail 2 is
Disk 5
Disk 6
Disk 7
Disk 8

So far so good. I have my 180 GB LUNs. My first challenge is to append drive letters so that output of diskdetail2 (or some other file after string manipulation) looks like the following
Disk 5 E
Disk 6 F
Disk 7 G
Disk 8 H

The second part of the script is as follows

@Echo Now creating scripts for disk initialization
for /f "tokens=1,2 delims= " %%a in (diskdetail2.txt) do (
@echo select %%a %%b
@echo online disk
@echo attributes disk clear readonly
@echo clean
@echo convert mbr
@echo create partition primary
@echo select part 1
@echo format fs=ntfs quick
@echo assign letter E
@echo.
) >>diskpartscript.txt


The output script that I can use with diskpart command gives me only drive letter E. That is why I need the diskdetail2 file with drive letters so that I can assign them here.
The second challenge is that I want to create 4 diskpart scripts instead of one. With just one script I am running into some diskpart issues that I think will not happen if I execute 4 scripts with a gap of 10 seconds each.

Any help is highly appreciated!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Trying to create DISKPART script using batch file

#2 Post by foxidrive » 10 May 2014 07:19

A_Bobby wrote:the output I get in diskdetail 2 is
Disk 5
Disk 6
Disk 7
Disk 8

So far so good. I have my 180 GB LUNs. My first challenge is to append drive letters so that output of diskdetail2 (or some other file after string manipulation) looks like the following

Disk 5 E
Disk 6 F
Disk 7 G
Disk 8 H




Can you clarify what you are after?

Will you have Disk N on separate lines and you want to add drive letters starting from E ?

A_Bobby
Posts: 38
Joined: 21 Oct 2010 12:48

Re: Trying to create DISKPART script using batch file

#3 Post by A_Bobby » 10 May 2014 08:28

Thanks for the reply!
If you look at the second part of the script you will see "assign letter E" command. This is a constant in the script. I cannot have all four drives with letter E. The first 180 GB disk (Disk 5) will be assigned E, disk 6 F and so on.
So in order to achieve this I have to manipulate diskdetail2.txt file and achieve end result that looks like
Disk 5 E
Disk 6 F
Disk 7 G
Disk 8 H

instead of just
Disk 5
Disk 6

If I get the desired output as shown with letters E, F, G and H I can then get three values using tokens=1,2,3 and assign the third value E to the line "assign letter %%c"

I hope this makes sense.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Trying to create DISKPART script using batch file

#4 Post by foxidrive » 10 May 2014 08:56

This will handle the situation you've described and write "diskdetail2.txt.tmp" and the I J K is just in case there are extra drives.

You can also use the loop directly to create your next script.

Code: Select all

@echo off
setlocal enabledelayedexpansion
(for %%a in (E F G H I J K) do echo %%a)>drv.tmp

<drv.tmp (for /f "usebackq delims=" %%a in ("diskdetail2.txt") do (
 set /p d=
 echo %%a !d!
)
)>"diskdetail2.txt.tmp"
del drv.tmp


Such as this:

Code: Select all

@echo off
setlocal enabledelayedexpansion
(for %%a in (E F G H I J K) do echo %%a)>drv.tmp

<drv.tmp (for /f "usebackq tokens=1,2" %%a in ("diskdetail2.txt") do (
 set /p d=
@echo select %%a %%b
@echo online disk
@echo attributes disk clear readonly
@echo clean
@echo convert mbr
@echo create partition primary
@echo select part 1
@echo format fs=ntfs quick
@echo assign letter !d!
@echo.
)
)>"diskpartscript.txt"
del drv.tmp

A_Bobby
Posts: 38
Joined: 21 Oct 2010 12:48

Re: Trying to create DISKPART script using batch file

#5 Post by A_Bobby » 10 May 2014 13:57

Awesome! thanks a lot!
The diskpartscript.txt handles all four drive letters. Is there a way to create 4 separate scripts?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Trying to create DISKPART script using batch file

#6 Post by foxidrive » 10 May 2014 23:37

This creates separate scripts.

Code: Select all

@echo off
setlocal enabledelayedexpansion
(for %%a in (E F G H I J K) do echo %%a)>drv.tmp

<drv.tmp (for /f "usebackq tokens=1,2" %%a in ("diskdetail2.txt") do (
 set /p d=
(
@echo select %%a %%b
@echo online disk
@echo attributes disk clear readonly
@echo clean
@echo convert mbr
@echo create partition primary
@echo select part 1
@echo format fs=ntfs quick
@echo assign letter !d!
@echo.
)>"diskpartscript-drive letter !d!.txt"
)
)
del drv.tmp

Post Reply