There are several ways you can determine the virtualization host (server with the Hyper-V role enabled), also called the physical host, that a VM is running on.
If the host OS is Windows Server 2008 R2, and the virtual machines on it are using the R2 integrations services, then you can query the following:
$[HKLM\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters] "HostName"= "PhysicalHostName"=" "PhysicalHostNameFullyQualified"= "VirtualMachineName"=
Get-VMMServer cluster.domain.com | Get-VM Server1 | Select-Object vmhost Replace cluster.domain.com to the FQDN of your VMM cluster and Server1 to the server you're looking for.
This script pulls the host name from the VM's registry.
function Get-VMOSDetail
{
Param(
[Parameter()]
$ComputerName = $Env:ComputerName,
$VMName
)
# Creating HASH Table for object creation
$MyObj = @{}
# Getting VM Object
$Vm = Get-WmiObject -Namespace root\virtualization -Query
"Select * From Msvm_ComputerSystem Where ElementName='$VMName'"
-ComputerName $ComputerName
# Getting VM Details
$Kvp = Get-WmiObject -Namespace root\virtualization -Query
"Associators of {$Vm} Where AssocClass=Msvm_SystemDevice ResultClass=Msvm_KvpExchangeComponent"
# Converting XML to Object
foreach
($CimXml
in
$Kvp.GuestIntrinsicExchangeItems)
$XML = [XML]$CimXml
if
($XML)
($CimProperty
$XML.SelectNodes(
"/INSTANCE/PROPERTY"
))
switch
-exact ($CimProperty.Name)
"Data"
{ $Value = $CimProperty.VALUE }
"Name"
{ $Name = $CimProperty.VALUE }
}
$MyObj.add($Name,$Value)
# Outputting Object
New-Object -TypeName PSCustomObject -Property $MyObj
BSonPosh edited Revision 9. Comment: Adding My code to get VMOSDetail
John Grenfell edited Revision 8. Comment: Sample code to get the host of a vm on a vmm cluster - my first wiki post - please be gentle!
Saud AlMishari MSFT edited Revision 4. Comment: Updated article to clarify that SQL query is for ConfigMgr only.
Tony Soper_MSFT edited Revision 1. Comment: added get count of guests by host script
Tony Soper_MSFT edited Original. Comment: Added PSH script and Resources
hi, this is good info. however, is there any way for me to check the "history" host owner of one particular virtual machine?
Raymond: Please clarify - are you looking for the "owner" of a virtual machine, regardless of the host it is currently on, or may have been on in the past?
Or,
Are you looking for a list of all the hosts a particular VM has been on?
AFAIK there is no way to track "host history" of a VM *on* or "from" the VM. If you have the required credentials and access to the *host* log files, you should be able to parse and track who/what regarding any VM on the host.
hi tonyso, thanks for ur feedbac. i m looking for the history (past) host where my desire VM located.... looks there is no other way besides checking from cluster.log ...
Hi tonysoper, mind to share the step?
Raymond, If I understand correctly what you are trying to do, there is no way to do that in Hyper-V. I am not positive, but I don't think there is a way in SCVMM either. You should check with the SCVMM community just to be sure, try social.technet.microsoft.com/.../virtualmachinemanager
The PowerShell above can be replaced with a one-liner:
(get-item "HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters").GetValue("HostName")
It's really great!
thanks for sharing, helpful post
nice article