Sergey Nivens - Fotolia

Tip

Useful PowerCLI cmdlets for managing hard disks

Users can more easily manage a VM's hard disk with a few common PowerCLI cmdlets. PowerCLI enables users to create, modify, move and remove disks so as to automate complex tasks.

With PowerCLI, admins can manage and automate many different facets of vSphere. Just a few cmdlets enable VMware admins to create, modify, move and remove disks.

The advantage VMware provides with PowerCLI is the ability to automate complex tasks without spending time in a GUI. But, before admins incorporate PowerCLI into their arsenal of tools, they must understand the different cmdlets that are used for this automation.

Viewing hard disks

Admins can use a number of methods to display information regarding vSphere hard disks, but the Get-HardDisk cmdlet provides insightful data, such as datastore, capacity, connection state and location.

To view all attached hard disks for a particular VM, an admin can use the following commands:

PS /Users/dan/ > Get-HardDisk -VM Test-VM | Select-Object Name,StorageFormat,Persistence,Filename,CapacityGB


Name          : Hard disk 1
StorageFormat : Thin
Persistence   : Persistent
Filename      : [Test-1] Test-VM_1/Test-VM_3.vmdk
CapacityGB    : 110
 
Name          : Hard disk 2
StorageFormat : Thin
Persistence   : Persistent
Filename      : [Test-1] Test-VM_1/Test-VM_1_1.vmdk
CapacityGB    : 1843

Admins can pipe the output to Select-Object, which enables them to see whatever property they wish to single out. Admins can also show disks for all VMs in a cluster by using the Get-Cluster cmdlet:

PS /Users/dan> Get-HardDisk -VM (Get-Cluster -Name 'Cluster' | Get-VM) | Select Parent,CapacityGB | Sort-Object -Property Parent
 
Parent                   CapacityGB
------                   ----------
test                     40
test-2                   110
test-3                   1843
test-4                   380
test-5                   1536

Creating hard disks

To create new hard disks from scratch, admins can use the New-HardDisk cmdlet. If admins want to create a new disk with 5 GB capacity on the VM Test-1, for example, they should also specify what datastore the disk should reside on, which is Test-1.

PS /Users/dan> New-HardDisk -VM Test-VM -CapacityGB 5 -DataStore Test-1

CapacityGB      Persistence        Filename
----------      -----------        --------
5.000           Persistent         [Test-1] Test-VM_1/Test-VM.vmdk

Modifying hard disks

To modify an existing hard disk, admins should use the cmdlet Set-HardDisk. This cmdlet enables admins to modify such factors as the datastore a disk resides on, a disk's capacity and whether to inflate or zero out a disk.

Admins can use Set-HardDisk to increase a disk's capacity to 10 GB, for example:

PS /Users/dan> Get-HardDisk -VM Test-VM -Name 'Hard disk 3' | Set-HardDisk -CapacityGB 10

Confirm
Are you sure you want to perform this action?
Performing the operation "Setting CapacityGB: 10." on target "Hard disk 3".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend [?] Help (default is "Y"): a

CapacityGB      Persistence      Filename
----------      -----------      --------
10.000          Persistent       [Test-1] Test-VM_1/Test-VM.vmdk

Storage vMotion

These useful PowerCLI cmdlets enable admins to perform a storage VMotion migration to a separate datastore for a particular disk. Admins can also move many disks to different datastores at once. Admins can use the following command to move, for example, Hard disk 3 to the datastore Test-2:

PS /Users/dan> Get-HardDisk -VM Test-VM -Name 'Hard disk 3' | Move-HardDisk -Datastore 'Test-2'

Admins can even move all hard disks for a specified VM:

PS /Users/dan> Get-HardDisk -VM Test-VM  | Move-HardDisk -Datastore 'Test-2'

Removing hard disks

When VMs stop being useful, admins might want to remove that data. Admins can remove hard disks by including the -DeletePermanently parameter when eliminating a VM with Remove-VM:

PS /Users/dan> Remove-VM -VM Test-VM -DeletePermanently

Admins can also use the Remove-HardDisk cmdlet. For example, an admin can remove all hard disks for the VM Test-VM:

Get-HardDisk -VM Test-VM | Remove-HardDisk

Dig Deeper on VMware ESXi, vSphere and vCenter

Virtual Desktop
Data Center
Cloud Computing
Close