Get-MrVmHost.ps1

#Requires -Version 3.0
function Get-MrVmHost {

<#
.SYNOPSIS
    Determines the HyperV host virtualization server for the specified virtual machines.
  
.DESCRIPTION
    Get-MrVmHost is an advanced function for determining the HyperV host virtualiztion server for one
    or more VMs (virtual machines).
  
.PARAMETER ComputerName
    The name of the VM (virtual machine) to determine the HyperV host for.
 
.PARAMETER Credential
    Specifies a user account that has permission to perform this action. The default is the current user.
 
    Type a user name, such as User01 or Domain01\User01. Or, enter a PSCredential object, such as one generated by the
    Get-Credential cmdlet. If you type a user name, this cmdlet prompts you for a password.
  
.EXAMPLE
     Get-MrVmHost -ComputerName Server01, Server02, Server03
 
.EXAMPLE
     Get-MrVmHost -ComputerName Server01, Server02, Server03 -Credential (Get-Credential)
 
.INPUTS
    None
  
.OUTPUTS
    PSCustomObject
  
.NOTES
    Author: Mike F Robbins
    Website: http://mikefrobbins.com
    Twitter: @mikefrobbins
#>


    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [Alias('VMName')]       
        [string[]]$ComputerName,

        [System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty
    )

    $Params = @{
        ComputerName = $ComputerName
        ScriptBlock = {Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters'}
        ErrorAction = 'SilentlyContinue'
        ErrorVariable = 'Problem'
    }

    if ($PSBoundParameters.Credential) {
        $Params.Credential = $Credential
    }

    Invoke-Command @Params |
    Select-Object -Property VirtualMachineName, HostName

    foreach ($p in $Problem) {
        if ($p.origininfo.pscomputername) {
            Write-Warning -Message "Unable to read registry key on $($p.origininfo.pscomputername)" 
        }
        elseif ($p.targetobject) {
            Write-Warning -Message "Unable to connect to $($p.targetobject)"
        }
    }

}