Trying to create DISKPART script using batch file
Posted: 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!
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!