If you are like me and you run a large Hyper-V machine with several arrays attached to it I'm sure at some point you've asked or been asked to find out, what virtual machines are on a particular array and what resources they are using. The Hyper-V UI doesn't help which means the PowerShell cmdlet is the answer. However, even using that it isn't straight forward requiring you to push through a command like 

Get-VM | Get-VMHardDiskDrive | where {$_.Path -Like "D:\Virtuals\*"} 

VMName   ControllerType ControllerNumber ControllerLocation DiskNumber Path                      
------   -------------- ---------------- ------------------ ---------- ----                      
ASERVER1       IDE            0                0                             D:\Virtuals\SERVER1.vhdx         
EMAIL1     IDE            1                1                             D:\Virtuals\EMAIL1.vhdx      
XSERVER1     IDE            0                0                             D:\Virtuals\XSERVER1.vhdx   

to get that information. Unfortunately that output just isn't particularly useful as it doens't show enough useful information, I want to know CPU, Memory etc.

So I've written a function that fixes my problem.

#This script is copyright Richard Harvey C2018, by downloading and using this function you agree to the EULA https://harveyit.co.uk/contact/eula
 Function Get-VMInLocation { 
 
 <# 
 .SYNOPSIS
 Quickly get a list of all VMs in a given location. 

 .DESCRIPTION 
 version 1.0.0.0
 Quickly get a list of all VMs in a given location. Output is the same as Get-VM.
 
 
 
 .EXAMPLE
 Get-VMInLocation E:
 Get-VMInLocation C:\myvms
 Get-VMInLocation \hyperv\
  
 .PARAMTER
 
 .NOTES 

.REMARKS

 .LINK 
 https://harveyit.co.uk/projects-and-guides/get-vminlocation
 
 #> 
 
    Param(
    [Parameter(Mandatory=$True)]
    [string]$path
    )
    $VMArray = @()
    if((Test-Path $path -ErrorAction Stop) -eq $true)
        {
            Get-VM | Get-VMHardDiskDrive | where {$_.Path -Like "$path*"} | Select-Object VMName,path | ForEach-Object {

                $VMObject = New-Object System.Object
                $VMName = $_.VMName
                $VMPath = $_.Path
                $VMObject | Add-Member -Type NoteProperty -Name Name -Value "$VMName"
                $VMObject | Add-Member -Type NoteProperty -Name drive -Value "$VMPath"
                $VMObject | Add-Member -Type NoteProperty -Name State -Value ((Get-VM $_.VMName | Select-Object state) -replace "@{State=","" -replace "}","")
                $VMObject | Add-Member -Type NoteProperty -Name CPUUsage% -Value ((Get-VM $_.VMName | Select-Object CPUUsage) -replace "@{CPUUsage=","" -replace "}","%")
                $VMObject | Add-Member -Type NoteProperty -Name 'MemoryAssigned(MB)' -Value (((Get-VM $_.VMName | Select-Object MemoryAssigned) -replace "@{MemoryAssigned=","" -replace "}","")/1MB)
                $VMObject | Add-Member -Type NoteProperty -Name Uptime -Value ((Get-VM $_.VMName | Select-Object Uptime) -replace "@{Uptime=","" -replace "}","")
                $VMObject | Add-Member -Type NoteProperty -Name Status -Value ((Get-VM $_.VMName | Select-Object Status) -replace "@{Status=","" -replace "}","")
                $VMObject | Add-Member -Type NoteProperty -Name Version -Value ((Get-VM $_.VMName | Select-Object Version) -replace "@{Version=","" -replace "}","")       

                $VMArray += $VMObject
                }
        }
        else
            {
                Write-Host $path is invalid path -ForegroundColor Red
            }

           $VMArray 
}

Loading Conversation