Share via


VM Host

Here’s the one-liner:

reg query "HKLM\Software\Microsoft\Virtual Machine\Guest\Parameters" /v hostname

Here’s the wrapper

 function Get-VmHostName
{
    <#
        A fancy way to run remotely

        reg query "HKLM\Software\Microsoft\Virtual Machine\Guest\Parameters" /v hostname
    #>
    param([string[]]$ComputerName = @($env:COMPUTERNAME));

    foreach ($myComputerName in ($ComputerName | ? { $_; }))
    {
        if ($result = (reg query "\\$myComputerName\HKLM\Software\Microsoft\Virtual Machine\Guest\Parameters" /v hostname | select-string hostname) -replace '\s*$' -replace '.*\s' )
        {
            New-Object -TypeName PsObject | 
            Select-Object -Property @{
                n = 'ComputerName';
                e = { $myComputerName; }
            }, @{
                n = 'VMHostName';
                e = { $result.ToLower(); }
            }
        }
        else
        {
            Write-Warning "$($MyInvocation.MyCommand.Name) -ComputerName $myComputerName failed to return data."
        }
    }
}