Monthly Archive

Categories

Storage

More diskinfo

Yesterday I showed how to get the disk, partition and logical disk information using CIM. Today I want to show more diskinfo techniques.

This time we’ll use the Storage module which was introduced with Windows 8. Underneath the covers it uses CIM – just different classes. The storage module doesn’t differentiate between volumes  and logical disks – it just uses volumes.

To start at the physical disk and get the partition and volumes:

$diskinfo = Get-Disk | foreach {

  $parts = Get-Partition -DiskNumber $psitem.DiskNumber | where DriveLetter

  $disk = $psitem

  foreach ($part in $parts) { 
    
    Get-Volume -Partition $part | 
    foreach { 
      $props = $null

      $props = [ordered]@{ 
        Disk = $disk.Number 
         Model = $disk.Model 
        Firmware = $disk.FirmwareVersion 
        SerialNUmber = $disk.SerialNumber 
        'DiskSize(GB)' = [math]::Round(($disk.AllocatedSize / 1GB ), 2) 
        Partitions = $disk.NumberOfPartitions 
        Partition = $part.PartitionNumber 
        BootPartition = $part.IsBoot 
        'PartitionSize(GB)' = [math]::Round(($part.Size / 1GB ), 2) 
        VolumeBlockSize = $psitem.AllocationUnitSize 
        LDiskName = $psitem.DriveLetter 
        FileSystem = $psitem.FileSystem 
         LDiskSize =  [math]::Round(($psitem.Size / 1GB ), 2) 
        LDiskFree =  [math]::Round(($psitem.SizeRemaining / 1GB ), 2) 
       }

      New-Object -TypeName PSObject -Property $props 
    } 
  } 
 } 
 $diskinfo

And to go the other way

$diskinfo =  Get-Volume | 
 where {$_.DriveLetter -AND $_.DriveType -eq 'Fixed'} | 
foreach {

      $part = Get-Partition -DriveLetter $psitem.DriveLetter 
       
      $disk = Get-Disk -Partition $part

      $props = $null

      $props = [ordered]@{ 
        Disk = $disk.Number 
         Model = $disk.Model 
        Firmware = $disk.FirmwareVersion 
        SerialNUmber = $disk.SerialNumber 
        'DiskSize(GB)' = [math]::Round(($disk.AllocatedSize / 1GB ), 2) 
        Partitions = $disk.NumberOfPartitions 
        Partition = $part.PartitionNumber 
        BootPartition = $part.IsBoot 
        'PartitionSize(GB)' = [math]::Round(($part.Size / 1GB ), 2) 
         VolumeBlockSize = $psitem.AllocationUnitSize 
        LDiskName = $psitem.DriveLetter 
         FileSystem = $psitem.FileSystem 
        LDiskSize =  [math]::Round(($psitem.Size / 1GB ), 2) 
        LDiskFree =  [math]::Round(($psitem.SizeRemaining / 1GB ), 2) 
      }

      New-Object -TypeName PSObject -Property $props 
 } 
 $diskinfo

The number of blocks doesn’t seem to be available – suppose you could calculate it – otherwise the information is the same as with the CIM classes you saw last time. Some of the property names are different.

Linking disks, partitions and logical drives

A question of the forums was asking about discovering disk information. They were trying to pipe the output of Get-WmiObject into another Get-WmiObject. that won’t work. There is another way. On Windows machines physical drives are divided into 1 or more partitions which are each divided into one or more logical disks. Linking disks, partitions and logical drives is a relatively simple process.

You can start at the physical disk and work down to the logical disks or start at the logical disk and work back to the physical disk. Lets start with the logical disk.

$diskinfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType = 3" | 
foreach { 
  $props = $null 
  
  $part = Get-CimAssociatedInstance -InputObject $psitem -ResultClass Win32_DiskPartition 
  $disk = Get-CimAssociatedInstance -InputObject $part -ResultClassName Win32_DiskDrive 
  
  $props = [ordered]@{ 
     Disk = $disk.Index 
     Model = $disk.Model 
     Firmware = $disk.FirmwareRevision 
     SerialNUmber = $disk.SerialNumber 
     'DiskSize(GB)' = [math]::Round(($disk.Size / 1GB ), 2) 
     Partitions = $disk.Partitions 
     Partition = $part.index 
     BootPartition = $part.BootPartition 
     'PartitionSize(GB)' = [math]::Round(($part.Size / 1GB ), 2) 
     Blocks = $part.NumberOfBlocks 
     BlockSize = $part.BlockSize 
     LDiskName = $psitem.Caption 
     FileSystem = $psitem.FileSystem 
      LDiskSize =  [math]::Round(($psitem.Size / 1GB ), 2) 
     LDiskFree =  [math]::Round(($psitem.FreeSpace / 1GB ), 2) 
  }

  New-Object -TypeName PSObject -Property $props

}

$diskinfo

Use Get-CimInstance to retrieve the instances of the Win32_LogicalDisk class. Use a filter for DriveType = 3 – which is local disks (as far as the server is concerned – they could be on a SAN or NAS).

Foreach of the disks get the associated partition and use that object to get the associated physical drive.

CIM (WMI) has the concept of associators and references.

A reference is a pointer showing you which instance is associated with another instance. For example:

PS> Get-CimInstance -ClassName Win32_LogicalDiskToPartition


Antecedent      : Win32_DiskPartition (DeviceID = "Disk #0, Partition #1") 
Dependent       : Win32_LogicalDisk (DeviceID = "C:") 
EndingAddress   : 511578663935 
StartingAddress : 368050176 
PSComputerName  :

Logical disk C: is associated with partition #1 on disk #0

If you want to actually get the associated class then you do this

PS> $ld = Get-CimInstance -ClassName Win32_LogicalDisk -Filter 'DeviceID = "C:"' 
 PS> Get-CimAssociatedInstance -InputObject $ld -ResultClass Win32_DiskPartition

Name             NumberOfBlocks       BootPartition        PrimaryPartition     Size                Index 
 ----             --------------       -------------        ----------------     ----                ----- 
 Disk #0, Part... 998458230            False                True                 511210613760        1

or

PS> Get-CimInstance -ClassName Win32_LogicalDisk -Filter 'DeviceID = "C:"' |   Get-CimAssociatedInstance -ResultClass Win32_DiskPartition

Name             NumberOfBlocks       BootPartition        PrimaryPartition     Size                Index 
 ----             --------------       -------------        ----------------     ----                ----- 
 Disk #0, Part... 998458230            False                True                 511210613760        1

Once you’ve go the partition and physical disk instances. Populate your output object and loop. Notice that the pipeline is output directly to the variable $diskinfo. You don’t need to build arrays – get the pipeline to do it for you.

Each logical disk gets an output like this

Disk              : 0 
 Model             : Samsung SSD 840 PRO Series 
 Firmware          : DXM06B0Q 
SerialNUmber      : S1AXNSAF329511V 
DiskSize(GB)      : 476.93 
 Partitions        : 3 
 Partition         : 1 
BootPartition     : False 
PartitionSize(GB) : 476.1 
 Blocks            : 998458230 
BlockSize         : 512 
LDiskName         : C: 
FileSystem        : NTFS 
LDiskSize         : 476.1 
LDiskFree         : 212.33

That’s working up the stack. What about working down. That’s a similar process:

$diskinfo = Get-CimInstance -ClassName Win32_DiskDrive | 
foreach { 
  $disk = $psitem 
  
  $parts = Get-CimAssociatedInstance -InputObject $psitem -ResultClass Win32_DiskPartition

  foreach ($part in $parts) { 
    
    Get-CimAssociatedInstance -InputObject $part -ResultClassName Win32_LogicalDisk | 
    foreach { 
       $props = $null

      $props = [ordered]@{ 
        Disk = $disk.Index 
        Model = $disk.Model 
        Firmware = $disk.FirmwareRevision 
        SerialNUmber = $disk.SerialNumber 
        'DiskSize(GB)' = [math]::Round(($disk.Size / 1GB ), 2) 
        Partitions = $disk.Partitions 
        Partition = $part.index 
        BootPartition = $part.BootPartition 
         'PartitionSize(GB)' = [math]::Round(($part.Size / 1GB ), 2) 
        Blocks = $part.NumberOfBlocks 
        BlockSize = $part.BlockSize 
        LDiskName = $psitem.Caption 
        FileSystem = $psitem.FileSystem 
        LDiskSize =  [math]::Round(($psitem.Size / 1GB ), 2) 
        LDiskFree =  [math]::Round(($psitem.FreeSpace / 1GB ), 2) 
      }

      New-Object -TypeName PSObject -Property $props 
    } 
  } 
 } 
 $diskinfo

Start with getting the instances of Win32_Diskdrive. Foreach instance get the associated partitions - Win32_DiskPartition.

Iterate through the partitions and get the associated logical disk. Create your object and output.

NOTE: neither of these techniques will show the partitions that don’t contain logical drives so you won’t see the boot partition and other “hidden partitions” on modern Windows machines. if you need those look at Win32_DiskPartition directly.

Get-PhysicalDisk options

These are the Get-PhysicalDisk options for identifying the disk you want

-UniqueId <string>

-ObjectId <string>

-FriendlyName <string>

-InputObject <CimInstance#MSFT_PhysicalDisk>

-StorageSubsystem <CimInstance#MSFT_StorageSubsystem>

-StorageEnclosure <CimInstance#MSFT_StorageEnclosure>

-StorageNode <CimInstance#MSFT_StorageNode>

-StoragePool <CimInstance#MSFT_StoragePool>

-VirtualDisk <CimInstance#MSFT_VirtualDisk>

When dealing with disks installed in the machine then the friendly names is the easiest to use

PS> Get-PhysicalDisk | Format-List UniqueId, ObjectId, FriendlyName

UniqueId     : 60022480233DF060FE631B8A4EDD93A0
ObjectId     : {1}\\W510W16\root/Microsoft/Windows/Storage/Providers_v2\SPACES_PhysicalDisk.ObjectId="{1dab9cf6-a1b4-11e6-a890-806e6f6e6963}:PD:{12e941a8-6125-c008-8806-8868642331ef}"
FriendlyName : Msft Virtual Disk

UniqueId     : {d8e80f34-22bc-0a36-b302-d96abe30a6cc}
ObjectId     : {1}\\W510W16\root/Microsoft/Windows/Storage/Providers_v2\SPACES_PhysicalDisk.ObjectId="{1dab9cf6-a1b4-11e6-a890-806e6f6e6963}:PD:{d8e80f34-22bc-0a36-b302-d96abe30a6cc}"
FriendlyName : Samsung SSD 840 PRO Series

Diskpart and PowerShell – part 6: Multiple partitions on a disk

So far we’ve looked at creating a single partition on a disk. This time we’ll look at how you can create multiple partitions on a disk. The are good reasons not to do this but its something I’ve seen done on a frequent basis.

Lets create a 20GB disk as an example and mount it

New-VHD -Path C:\test\Test1.vhdx -Dynamic -SizeBytes 20GB 
Get-VHD -Path C:\test\Test1.vhdx | Mount-VHD

 

Initialise the disk

Initialize-Disk -Number 1

 

Now we can create some partitions

New-Partition -DiskNumber 1 -DriveLetter F -Size 5GB 
New-Partition -DiskNumber 1 -DriveLetter G -Size 5GB 
New-Partition -DiskNumber 1 -DriveLetter H -Size 5GB 
New-Partition -DiskNumber 1 -DriveLetter I -Size 4.87GB

 

The reason that the last partition is only 4.87 G is that 128MB of disk space is reserved

PS> Get-Partition -DiskNumber 1 | Format-Table -AutoSize


   DiskPath: \\?\scsi#disk&ven_msft&prod_virtual_disk#2&1f4adffe&0&000003#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}

PartitionNumber DriveLetter Offset         Size Type 
--------------- ----------- ------         ---- ---- 
1                           17408        128 MB Reserved 
2               F           135266304      5 GB Basic 
3               G           5503975424     5 GB Basic 
4               H           10872684544    5 GB Basic 
5               I           16241393664 4.87 GB Basic

 

You can format the 4 new volumes in one pass

Get-Partition -DiskNumber 1 | 
where Type -ne 'Reserved' | 
Format-Volume -FileSystem NTFS -Confirm:$false –Force

 

PS> Get-Partition -DiskNumber 1 | Get-Volume | select DriveLetter, FileSystem, Size

DriveLetter FileSystem       Size 
----------- ----------       ---- 
          H NTFS       5368705024 
          G NTFS       5368705024 
          I NTFS       5229244416 
          F NTFS       5368705024

 

The Storage module can be used to simply and easily create multiple volumes on a disk

Diskpart and PowerShell – part4: Remove a partition

So far you’ve seen how to create and modify partitions and volumes. Its now time to look at how you remove a partition.

Mount the test VHD

Get-VHD -Path C:\test\Test1.vhdx | Mount-VHD

 

You can’t remove a volume – you have to remove the partition. Identifying the CORRECT partition to remove is the challenge

PS> Get-Partition | select PartitionNumber, DriveLetter, Size, Type

PartitionNumber DriveLetter         Size Type 
--------------- -----------         ---- ---- 
              1                134217728 Reserved 
              2           F   8589934592 Basic 
              1                367001600 IFS 
              2           C 511269232640 IFS 
              3                470810624 Unknown

 

Not every partition has a drive letter and partition numbers are repeated. The partition object holds the disk number

PS> Get-Partition | select DiskNumber, PartitionNumber, DriveLetter, Size, Type | Format-Table

DiskNumber PartitionNumber DriveLetter         Size Type 
---------- --------------- -----------         ---- ---- 
         1               1                134217728 Reserved 
         1               2           F   8589934592 Basic 
         0               1                367001600 IFS 
         0               2           C 511269232640 IFS 
         0               3                470810624 Unknown

 

So the combination of disk number and partition number is unique and will identify any partition. Remove our 8GB partition

Remove-Partition -DiskNumber 1 -PartitionNumber 2 -Confirm:$false

and the 128MB partition

Remove-Partition -DiskNumber 1 -PartitionNumber 1 -Confirm:$false

 

Get-Partition will show that the drive F: has been removed

Looking the disk organisation

Get-Disk -Number 1 | Select @{N='Size'; E={[math]::Round(($_.Size / 1GB), 2)}}, @{N='AllocatedSize'; E={[math]::Round(($_.AllocatedSize / 1GB), 2)}}, @{N='LargestFreeExtent'; E={[math]::Round(($_.LargestFreeExtent / 1GB), 2)}} | Format-List

Size              : 20 
AllocatedSize     : 0 
LargestFreeExtent : 20

 

The whole of the disk is now available for re-use

Diskpart and PowerShell–part 4: Expand a volume

Let’s create a new disk and mount it

New-VHD -Path C:\test\Test1.vhdx -Dynamic -SizeBytes 20GB 
Get-VHD -Path C:\test\Test1.vhdx | Mount-VHD 
Initialize-Disk -Number 1

 

This time we’ll create a volume that only uses part of the disk

New-Partition -DiskNumber 1 -DriveLetter F -Size 5GB

 

And now format the partition

Get-Partition -DriveLetter F | 
Format-Volume -FileSystem NTFS -Confirm:$false –Force

 

The disk is organised like this

Get-Disk -Number 1 | 
Select @{N='Size'; E={[math]::Round(($_.Size / 1GB), 2)}}, 
@{N='AllocatedSize'; E={[math]::Round(($_.AllocatedSize / 1GB), 2)}}, 
@{N='LargestFreeExtent'; E={[math]::Round(($_.LargestFreeExtent / 1GB), 2)}} | 
Format-List

 

Size              : 20 
AllocatedSize     : 5.13 
LargestFreeExtent : 14.87

 

Lets expand the partition

Get-Partition -DriveLetter F | 
Resize-Partition -Size 8GB

 

And re-examine the disk organisation

Get-Disk -Number 1 | 
Select @{N='Size'; E={[math]::Round(($_.Size / 1GB), 2)}}, 
@{N='AllocatedSize'; E={[math]::Round(($_.AllocatedSize / 1GB), 2)}}, 
@{N='LargestFreeExtent'; E={[math]::Round(($_.LargestFreeExtent / 1GB), 2)}} | 
Format-List

Size              : 20 
AllocatedSize     : 8.13 
LargestFreeExtent : 11.87

 

The extra space is added to the volume and formatted to match the existing filesystem on the volume

Mass dismount VHDs

I’m going to be creating, using and discarding a number of VHDs for my diskpart and PowerShell series. When I have a number of them mounted I want a quick way to dismount them. Assuming I consistently keep them in the same folder then this very nicely does the job

Get-ChildItem -Path C:\test\ -Filter *.vhdx | Dismount-VHD

 

Why does it work?

Because Get-ChildItem emits System.IO.FileInfo objects that have a Path property and Dismount-VHD accepts pipeline input for the Path of the VHD to dismount:

-Path <String[]>
    Specifies one or more virtual hard disk files for which the corresponding virtual hard disks are to be dismounted.

    Required?                    true
    Position?                    1
    Default value                none
    Accept pipeline input?       true (ByValue, ByPropertyName)
    Accept wildcard characters?  false

Diskpart and PowerShell–part 3: Initialize disk and create volume

Last time we created a virtual disk and mounted it. In this post we’ll initialize the disk and create a volume.

Start by remounting the disk

Get-VHD -Path C:\test\Test1.vhdx | Mount-VHD

 

You can now initialize the disk:

Initialize-Disk -Number 1

 

Create a partition:

New-Partition -DiskNumber 1 -DriveLetter F –UseMaximumSize

 

Ignore the message about formatting as you want to control that:

Format-Volume -DriveLetter F -FileSystem NTFS -Confirm:$false –Force

 

Your new disk is ready to use.

The diskpart equivalents can be found here: https://technet.microsoft.com/en-us/library/cc766465(v=ws.10).aspx

 

You can perform the creation and formatting of the disk in one pass:

New-VHD -Path C:\test\Test2.vhdx -Dynamic -SizeBytes 10GB |
Mount-VHD -Passthru |
Initialize-Disk -PassThru |
New-Partition -DriveLetter G -UseMaximumSize |
Format-Volume -FileSystem NTFS -Confirm:$false –Force

 

Parameterize the path, size and drive letter and you have a handy function to set up disks

Diskpart and PowerShell–part 2: Create a virtual disk

Before we start digging into the diskpart/Storage module functionality we need a disk to practice on. I don’t recommend using your machine’s system disk – bad things will happen.

The Hyper-V module has  a New-VHD cmdlet so lets use that to create a disk to play with. The great thing about virtual disks is that you can delete then if everything goes horribly wrong.

There is a New-VirtualDisk cmdlet in the storage module but that works with storage pools. We’ll cover that later in the series.

Lets create a virtual disk:

New-VHD -Path C:\test\Test1.vhdx -Dynamic -SizeBytes 10GB

 

You can access the virtual disk using the path

Get-VHD -Path C:\test\Test1.vhdx

 

You need to mount the virtual disk before you can work with it

Get-VHD -Path C:\test\Test1.vhdx | Mount-VHD

 

Once mounted you can use get-Disk to identify the virtual disk

PS> Get-Disk | select Number, FriendlyName, PartitionStyle

Number FriendlyName               PartitionStyle
------ ------------               --------------
1 Msft Virtual Disk          RAW
0 Samsung SSD 840 PRO Series MBR

 

The clue is in the friendly name and that the partition style is RAW.

Next time we’ll look at formatting and partitioning the new drive. For now we’ll just dismount the virtual disk

Dismount-VHD -DiskNumber 1