3

I'm trying to write a script to create and format a partition in Windows Server 2008R2.

Now, when disk 1 is selected, I need to format it, only if it is not formatted already. This is what I have now:

Run: diskpart /s script.txt

Content of script.txt

select disk 1
clean
create partition primary
format fs=ntfs unit=65536 quick
active
assign letter=D

Any help?

ccamacho
  • 203
  • 4
  • 10
  • You should be using Powershell as opposed to using DOS commands, Powershell allows for greater flexibility and will allow you to evaluate if a disk is not formatted then perform an action on it if the statement equals true. I assume you already know this though as you added a "Powershell" tag into your post. In any case, this command should get you started "Get-Command *disk*" – Mbond65 Aug 05 '14 at 11:36
  • You are right, the problem its that in Windows 2008R2 there are no Storage Cmdlets in Windows PowerShell. – ccamacho Aug 05 '14 at 13:19
  • Those diskpart replacements are only for v4 powershell and above. But don't worry, we can still check drive formatted status via Get-WmiObject cmdlet. Also look into the `Win32_Volume Format` method to avoid diskpart. – Knuckle-Dragger Aug 05 '14 at 16:36

2 Answers2

2

This works for me.

foreach ($disk in get-wmiobject Win32_DiskDrive -Filter "Partitions = 0"){ 
   $disk.DeviceID
   $disk.Index
   "select disk "+$disk.Index+"`r clean`r create partition primary`r format fs=ntfs unit=65536 quick`r active`r assign letter=D" | diskpart
}

In this case, i get the disk with NO partitions, and then i create a D drive with all available space

ccamacho
  • 203
  • 4
  • 10
0

I did this a while back using here strings to format the complete diskpart commands. It's a bit goofy looking but it worked at the time when we were trying to automate this quickly for a high number of luns on multi node clusters. the WMI method is definitely cleaner looking.

Gags
  • 31
  • 2