Get-VMGuestHardwareDetails.ps1


Description

Purpose

No synopsis provided.

Detailed Description

No detailed description provided.

Back to Top

Usage

No usage examples provided.

Back to Top

Notes

No additional notes.

Back to Top


Script

function Get-VMGuestHardwareDetails {
    [CmdletBinding()]
    Param(
        [Parameter(
            ValueFromPipeline            = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [string[]]$VMName
    )

    Process {
        foreach ($name in $VMName) {
            try {
                # This can return multiple VMs if $name is a wildcard
                $vmList = Get-VM -Name $name -ErrorAction Stop

                foreach ($vm in $vmList) {
                    # Build an array of disk info
                    $hardDisks = $vm.ExtensionData.Config.Hardware.Device |
                        Where-Object { $_.DeviceInfo.Label -like 'Hard disk*' } |
                        ForEach-Object {
                            [PSCustomObject]@{
                                Name       = $_.DeviceInfo.Label
                                CapacityGB = [Math]::Round($_.CapacityInKB / 1MB, 2)
                            }
                        }

                    # Build an array of NIC info (excluding NetworkName if you prefer)
                    $nicDevices = $vm.ExtensionData.Config.Hardware.Device |
                        Where-Object { $_ -is [VMware.Vim.VirtualEthernetCard] } |
                        ForEach-Object {
                            [PSCustomObject]@{
                                Name       = $_.DeviceInfo.Label
                                MacAddress = $_.MacAddress
                            }
                        }

                    # Retrieve IP addresses from the Guest property (can be null)
                    $ipList = if ($vm.Guest -and $vm.Guest.IPAddress) {
                        $vm.Guest.IPAddress
                    } else {
                        @('N/A')
                    }

                    # Construct the final custom object
                    $vmInfo = [PSCustomObject]@{
                        VMName      = $vm.Name
                        PowerState  = $vm.PowerState
                        NumCPU      = $vm.NumCPU
                        MemoryGB    = $vm.MemoryGB
                        GuestOS     = $vm.Guest.OSFullName
                        ToolsStatus = $vm.ExtensionData.Guest.ToolsStatus
                        IPAddresses = $ipList
                        HardDisks   = $hardDisks
                        Networks    = $nicDevices
                    }

                    # Emit the object
                    Write-Output $vmInfo
                }
            }
            catch {
                Write-Warning "Failed to retrieve details for VM '$name': $($_.Exception.Message)"
            }
        }
    }
}

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top