chelnak
8/2/2016 - 2:48 PM

Get-VMExtendedInfo.ps1

function Get-VMExtendedInfo {
<#
    .SYNOPSIS
    Gets extended information about a Virtual Machine    

    .DESCRIPTION
    Gets extended information about a Virtual Machine    

    .PARAMETER VM
    The Virtual Machine to query

    .INPUTS
    PSObject

    .OUTPUTS
    System.Management.Automation.PSObject

    .EXAMPLE
    Get-VM | Get-VMExtendedInfo | Export-Csv -Path .\VMInfo.csv -NoTypeInformation

    .EXAMPLE 
    Get-VM | Get-VMExtendedInfo

    .EXAMPLE
    Get-VM -Name VM-01 | Get-VMExtendedInfo

#>
[CmdletBinding()][OutputType('System.Management.Automation.PSObject')]

    Param (

        [parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [ValidateNotNullOrEmpty()]
        [PSObject[]]$VM

    )

    begin {}

    process{

        foreach ($VirtualMachine in $VM) {

            $View = $VirtualMachine | Get-View
            [URI]$ServiceUrl = $View.Client.ServiceUrl

            [PSCustomObject]@{

                Name = $VirtualMachine.Name
                PowerState = $View.Runtime.PowerState
                GuestOSFullName = $View.Guest.GuestFullName
                ParentHost = $VirtualMachine.VMHost.Name
                ParentvCenter = $ServiceUrl.Host
                ParentvCenterFolder = $VirtualMachine.Folder
                ParentDatacenter = ($VirtualMachine | Get-Datacenter).Name
                NumberOfNics = ($VirtualMachine | Get-NetworkAdapter).Count

            }

        }

    }

    end {}

}