Register for Semperis' Hybrid Identity Protection (HIP) Conference - June 30 - July 1 Register for Semperis' Hybrid Identity Protection (HIP) Conference - June 30 - July 1
Hyper-V|PowerShell

Testing Hyper-V Disk Health with a Custom Property

In a previous article, I demonstrated how you could use PowerShell to test the health of a Hyper-V virtual machine’s disk files. If you skipped that article, take a moment to read it, otherwise what I’m going to cover in this article might be confusing. The goal is to simplify typing or limit the amount of how much I have to type. I have a working script block that will give me a Boolean value on the existence of the VM’s vhd and vhdx files.


$testvhd = {
if ($this.HardDrives) {
#test if there are any false results
if ( ($this | Get-VMHardDiskDrive | Test-Path) -contains $False ) {
$False
}
else {
$True
}
}
else {
#no hard drive files configured
$false
}
}

This is essentially the same code that I used in my previous article. Although if you look closely, you’ll see I am using $this instead of $_. That’s because I intend to use this script block with Add-Member to create a custom property. Specifically, I am going to create a script property that will use the script block to grab the current value. In this situation, $this refers to the current object in the pipeline.
Get-VM | 
Add-Member -MemberType ScriptProperty -Name TestVHD -Value $Testvhd -PassThru -force |
Select Name,State,TestVHD,@{Name="Drives";Expression={$_.HardDrives.path}},Status |
Out-Gridview -title "VHD Test"

Testing VHD files with a custom property (Image Credit: Jeff Hicks)
Testing VHD files with a custom property (Image Credit: Jeff Hicks)

As an added benefit, the new property is now part of the virtual machine object.

Verifying the new property (Image Credit: Jeff Hicks)
Verifying the new property (Image Credit: Jeff Hicks)

Stated differently, I can use this property whenever I want, and I don’t need to use Add-Member again.

get-vm | where {$_.state -ne "running" -AND (-Not $_.TestVHD) } | 
Select Name,State,TestVHD,Status | format-table -AutoSize
Using the new property (Image Credit: Jeff Hicks)
Using the new property (Image Credit: Jeff Hicks)

This property will persist for the length of my PowerShell session. For something a bit more permanent, I can update the type configuration file and define my custom property. First, I need to know the type name of the Hyper-V virtual machine object.

Getting the virtual machine typename (Image Credit: Jeff Hicks)
Getting the virtual machine typename (Image Credit: Jeff Hicks)

This is the same information that you would see piping a VM to Get-Member. Because I want my new property to exist every time I start PowerShell, I’ll copy and paste my script block and this command into my profile script.

Update-TypeData -TypeName -MemberType ScriptProperty -membername TestVHD -Value $testvhd

The Update-TypeData cmdlet will extend the definition of the virtual machine object with my new property, which I can immediately verify with Get-Member.

Verifying the new property (Image Credit: Jeff Hicks)
Verifying the new property (Image Credit: Jeff Hicks)

This property is ready to use, but I’ll have to specify it.

Using the new custom property (Image Credit: Jeff Hicks)
Using the new custom property (Image Credit: Jeff Hicks)

And while I’m at it, I think I’ll add a custom property to give me the full path to the virtual machine’s XML configuration file, which is something else I demonstrated in the previous article.

Update-TypeData -TypeName Microsoft.HyperV.PowerShell.VirtualMachine -MemberType ScriptProperty -MemberName ConfigurationFile -value {Join-path -Path "$($this.configurationLocation)\Virtual Machines" -ChildPath "$($this.vmid).xml" -Resolve }
Using the new ConfigurationFile property (Image Credit: Jeff Hicks)
Using the new ConfigurationFile property (Image Credit: Jeff Hicks)

But why stop now? Let’s define a property that automatically expands the paths for all the disk files.

Update-TypeData -TypeName Microsoft.HyperV.PowerShell.VirtualMachine -MemberType ScriptProperty -MemberName DiskPath -value {$this.Harddrives.path}
Using the new DiskPath property (Image Credit: Jeff Hicks)
Using the new DiskPath property (Image Credit: Jeff Hicks)

Remember that you have to specify the property to see it, because it won’t be part of any default view. As luck would have it, you can create your own custom views with Update-FormatData. However, your head might be spinning a bit from everything I’ve showed you, so I’ll save that discussion for another article.

Related Topics:

BECOME A PETRI MEMBER:

Don't have a login but want to join the conversation? Sign up for a Petri Account

Register
Comments (0)

Leave a Reply

Register for the Hybrid Identity Protection (HIP) Europe Conference!

Hybrid Identity Protection (HIP) Europe 2021 - Virtual Conference

Mobile workforces, cloud applications, and digitalization are changing every aspect of the modern enterprise. And with radical transformation come new business risks. Hybrid Identity Protection (HIP) is the premier educational forum for identity-centric practitioners. At the inaugural HIP Europe, join your local IAM experts and Microsoft MVPs to learn all the latest from the Hybrid Identity world.