Provision New Virtual Hard Disks

When deploying a new hard drive or drives on a virtual machine, there are several steps to enable this storage in the OS of the VM. For Windows, these steps can all be accomplished via PowerShell. Granted, this is not a huge time saver on its own, but used in conjunction with a full configuration script, it’s one less thing you have to do manually to get your VM up and running.

# Detect, initialize and format unitialized drives
# -- Get a list of disks from the OS
$disks = get-disk
# -- Loop through each disk and do stuff
foreach ($disk in $disks) {
	# -- Get the size of the disk to use later
	$maxsize = $disk.Size
	# -- If the disk is offline and is cleared, set it online and do other stuff
	if ($disk.operationalstatus -eq "Offline" -and $disk.PartitionStyle -eq "RAW") {
		set-disk $disk.Number -IsOffline $False
		# -- If the disk is less than two terabytes, initialize as an MBR partition and format NTFS, if not, initialize as GPT and format NTFS.
		if ($maxsize -lt 2TB) {
			Initialize-Disk -Number $disk.Number -PartitionStyle MBR
			New-Partition -DiskNumber $disk.Number -UseMaximumSize -AssignDriveLetter | Format-Volume -FileSystem NTFS -confirm:$false
		Else {
			Initialize-Disk -Number $disk.Number -PartitionStyle GPT
			New-Partition -DiskNumber $disk.Number -UseMaximumSize -AssignDriveLetter | Format-Volume -FileSystem NTFS -confirm:$false

Tagged with: ,
Posted in Powershell

Leave a Reply

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

You are commenting using your 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: