Wednesday, December 17, 2014

Hard Disks with PowerShell

The other day, I needed to extend a Hyper-V iSCSI disk store (after increasing the size on the SAN).  I didn't know how.  One of our consultants used DISKPART - oh yeah - forgot that from NT 3.51 (or perhaps earlier).  I was using PowerShell's Get-Disk but wasn't able to go much further.  So I mocked up a VM to play with.  I added a drive and formatted it (after looking at the Hey, Scripting Guy! blog).
  PS  C:\Windows\system32> Get-Disk | Where partitionstyle -eq 'raw' | Initialize-Disk -PartitionStyle MBR -PassThru |New-Partition -AssignDriveLetter -UseMaximumSize |Format-Volume -FileSystem NTFS -NewFileSystemLabel "disk2" -Confirm:$false

You can run most of the commandlets between the pipes as individual commands (not sure that makes it any easier).

Anyway, once I had my test drive, it was time to expand it.  I halted the system and then made the changes in Citrix XenCenter.  After restarting, I checked to see that the drive was now 40GB instead of 25GB.  HOWEVER, this is misleading.  Although the DRIVE is now 40GB, the partition being used is still only 25GB.

PS C:\Windows\system32> Get-Partition -DiskNumber 1
Disk Number: 1

PartitionNumber DriveLetter  Offset       Size Type
--------------- -----------  ---------    ---- ----
1               E            1048576     25 GB IFS


To expand the disk, I had hoped there was a simple "MaxSize" but instead I found this:


PS C:\> $size = (Get-PartitionSupportedSize –DiskNumber 1 –PartitionNumber 1)

PS C:\> Resize-Partition -DiskNumber 1 –PartitionNumber 1 -Size $size.SizeMax

Ta-dahh!  Sadly, DISKPART is easier.  But, for the PowerShell purists, running Windows Core 2012 R2, this does the trick.

 
 

No comments: