Udostępnij za pośrednictwem


Hyper-V PowerShell One-Line-Challenge

While digging through my notes I came across an exchange between a colleague and I - where we were challenging each other to come up with the most impressive (and functional) "Hyper-V PowerShell One Liner".  Here is an example of a good one:

Get-Volume | ?{$_.DriveLetter -ne $null} | select @{N='Drive Letter';E={$_.DriveLetter}},@{N='Free Space (GB)';E={"{0:N2}" -f ($_.SizeRemaining / 1GB)}},@{N='Space Used By VHDs (GB)';E={$driveletter = $_.DriveLetter; "{0:N2}" -f ((Get-VMHardDiskDrive * | ? {$_.Path -match "^$driveletter" } | Get-VHD | Measure -sum FileSize | Select -exp sum)/1GB)}}

What does this one liner do?  Well - let's run it and see:

If you pull it apart you can see that what it does is:

  1. Get all disks in the physical computer
  2. List the drive letters of each disk
  3. Report the free space on each disk (formatted to gigabytes)
  4. Finds all virtual hard disks that Hyper-V knows about on each disk and displays how much space they are using (formatted to gigabytes)

Very nice!

Cheers,
Ben

Comments

  • Anonymous
    April 07, 2015
    Hi Ben, as tweeted earlier, this little PoSH 1-liner was very useful documenting a all VM's paused host, however a word of warning - it doesn't work quite so well on a cluster node, lists the volumes, but no vhdx/vhd sizing.  nice 1-liner all the same!  thx!

  • Anonymous
    April 10, 2015
    Hi Ben, is it possible to include CSVs in this? Best Regards, Jens