Thursday, February 14, 2013

Using the new storage cmdlets in Windows 2012

Starting with Windows 2012 there are a bunch of new Powershell cmdlets for disk and partition management. Their use is pretty straightforward and they can easily replace Server Manager as the tool to format partitions, reset disks or get stats about disk usage.

Nonetheless it takes some time to find the correct parameter to bind them one another with pipes. That's the reason why I have decided to write a little sample script where, starting from a list of server disks, I go down to find the partitions and the volumes on each of them.

The three cmdlets I used for this script are get-disk, get-partition and get-volume. Others exist and, if you want to play with them, the complete list of these 84 cmdlets is either on Technet or on Powershell V3.0  Get-Command –Module Storage.

Here's the script:
foreach($disk in (get-disk))
Write-Host "`nIn Disk $($disk.number) there are the following partitions:"
foreach($partition in (Get-partition -DiskNumber $disk.number | ?{$_.driveletter -match "^[a-z]*$"}))
write-host "`tIn Partition $($partition.partitionnumber) there are the following volumes:" -nonewline
write-Host "`t`t$(get-volume -driveletter $partition.driveletter | ft -hidetableheaders -property driveletter,filesystem -autosize | out-string)" -nonewline
A sample output would be:

In Disk 0 there are the following partitions:
   In Partition 4 there are the following volumes:
      C NTFS

In Disk 1 there are the following partitions:
   In Partition 2 there are the following volumes:
      E NTFS
      F NTFS
      G REFS

I haven't tested it on servers with volumes that span one or more drive, so if you do, please report whether this script works or not.

Last thing to know is that for the moment PowerGUI, if you use it, is not aware of those cmdlets, and fails importing the Storage module.

As a side note, I have had some trouble filtering out from my get-partition query those drives that have no letter assigned (such as the hidden system restore partition). I ended up using the -match operator with regular expressions (regex) as brilliantly explained  here.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...