RSS

CMD: Scripting Windows drive creation

08 Aug

For some reasons, our newly published (by our engineering team) server build program missed creating and formating the D drives on Windows 2000 servers. Of course, the simple solution it to just logon to the servers and format D: as normal to resolve this problem. But what if you have 30 or 100 servers that had the D: missing? This gives me another opportunity to test my scripting skills agian.

To create and partition the disk, I used DISPART.exe, which is a free MS util that you can download from, where else, MS website.

I used the following commands to create an new D partition:

SELECT Disk 1
Create Partition Primary
Assign letter=D

Unfortunately diskpart does not have a format function. For that, you have to use your old trusty FORMAT.EXE.

In command prompt, I typed the following command:

Format d: /fs:ntfs /v:DataD

But it gave me an error saying the volume or drive could not be found. I realise what happened quickly, I was logged on via terminal session and Windows 2000 did not refresh my session with the new drive information that FORMAT can use. I had to log off and log on again and rerun the command, which works. Now… I am even more determined to script this as I neither want to logon to console to do this one-by-one, nor log on, off and on just to create a D drive.

The problem with the FORMAT command is that it will prompt you with warning that you are going to erase all data. There are not quiet mode around this. To script it, you have to send the text “Y” to the command. Hence, the improved command is now:

type %~dp0ANS.txt | format d: /fs:ntfs /v:DataD

ANS.txt just contains “Y” with a carriage return.

For those who don’t know it %~dp0 is a variable substitution that substitutes the full path where your script is being run.

So I started up my task scheduling script and changed it so that it schedules diskpart to run first, then 2 minutes later, another task runs the format command. The 2nd task should get the new drive as it logs on to run the task.

Advertisements
 
Leave a comment

Posted by on August 8, 2007 in Windows, Windows CMD

 

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: