What physical computer am I on?
Once you start to get more and more virtual machines, and more and more Hyper-V servers, in your environment it can get quite hard to keep track of where a specific virtual machine is actually running.
System Center Virtual Machine Manager can help you out here – but what if you are not sitting at the System Center Virtual Machine Manager Console? What if you have used Remote Desktop to connect to the virtual machine? What do you do then?
A long, long time ago – I posted information on how to figure out the host operating system from inside a virtual machine using a VBScript on Virtual PC and Virtual Server. And this script also works on Hyper-V!
But no one uses VBScript anymore! Right? So how do we do this in PowerShell?
The answer is with a one-liner of course! (or actually – three one liners).
To get the name of the physical computer that you are running on, open a PowerShell inside the virtual machine and type in:
(Get-ItemProperty –path “HKLM:SOFTWAREMicrosoftVirtual MachineGuestParameters”).PhysicalHostName
You can also get the fully qualified name of the physical computer by running:
(Get-ItemProperty –path “HKLM:SOFTWAREMicrosoftVirtual MachineGuestParameters”).PhysicalHostNameFullyQualified
Finally, you can get the name of the virtual machine itself by running:
(Get-ItemProperty –path “HKLM:SOFTWAREMicrosoftVirtual MachineGuestParameters”).VirtualMachineName
And of course – this will work on Hyper-V, Virtual Server and Virtual PC.
Cheers,
Ben
Comments
Anonymous
January 07, 2011
What we need is a way for BGInfo to launch a PowerShell cmdlet so the info could be placed onto the backdropAnonymous
January 07, 2011
Maybe PSHyperv should have some simple cmdlets to install inside the VM to have something nicer like get-parent etc.Anonymous
January 07, 2011
Do you think storing physical machine fqdn on vm registry is a secure thing? You're leaving an open door for customers who use vm as a service, i think. Also what happens to that registery key if vm runs on hyperv cluster?Anonymous
January 07, 2011
The comment has been removedAnonymous
January 08, 2011
What's the difference between 'HostName' and 'PhysicalHostName' ? 10xAnonymous
January 09, 2011
isn't it a security problem? I mean it could be a potential hint for an attack vector.Anonymous
January 10, 2011
Dave Dustin - I agreee! Yusuf Ozturk / kirill kovalenko- We do not see this as a security risk. We have always designed our system with the assumption that the bad guy in a VM would be able to figure out that they were in a VM - and then ensure that there is nothing that they could do with that information. In this case - the only real risk is that a bad guy could try to launch a network based attack against the host (in which case - being in a VM gives them no advantage when compared to being a seperate physical computer) If this is a concern to you - there are two options you could take:
- Configure the virtual machines so that they cannot attempt to connect to the host over the network. To do this configure an "external only" network that connects to a different physical network than the one the host is connected to.
- Disable the data-exchange integration services under the virtual machine settings (which will stop this information from being sent in the first place). Ran - If you are in a cluster, HostName will return the cluster name and PhysicalHostName will return the node name. Otherwise they are the same. Cheers, Ben
- Anonymous
March 15, 2014
I have different virtual machines (ex: servers.txt) available and i need to get host name of all by passing the VM name.. using foreach or any other way ?