使用powershell在hyper-v(给定的VMname)上获取VM的主机名

时间:2022-01-11 15:33:05

My Hyper-v has virtual machines. Hyper-v knows only VM names . I want to extract hostname (which we get by running the 'hostname' command on cmd in virtual machine) on hyper-v , using powershell .

我的Hyper-v有虚拟机。 Hyper-v只知道VM名称。我想使用powershell在hyper-v上提取主机名(我们通过在虚拟机中的cmd上运行'hostname'命令获得)。

2 个解决方案

#1


0  

You do have integration components installed on the guest? If you don't, install those ASAP. After the IC are up to date, you can query the host info from VM's registry like so,

您确实在guest虚拟机上安装了集成组件吗?如果不这样做,请尽快安装。 IC是最新的,您可以从VM的注册表中查询主机信息,如下所示,

gp 'HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters'
HostName                           : hyperv100.contoso.local
HostingSystemEditionId             : 8
...
PhysicalHostName                   : hyperv100
PhysicalHostNameFullyQualified     : hyperv100.contoso.local
VirtualMachineName                 : guest101

(gp 'HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters').PhysicalHostName
hyperv100

(gp 'HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters').PhysicalHostNameFullyQualified
hyperv100.contoso.local

In addition, read an article that describes the IC registry keys.

此外,阅读描述IC注册表项的文章。

#2


0  

One way is to fetch the IP address of the VM and from that you can query for the hostname. I have used single command to find the hostname as:

一种方法是获取VM的IP地址,然后您可以查询主机名。我使用单个命令来查找主机名:

$VMName = "TestVMName"
$host_name = ((Get-VM | where { $_.Name -eq $VMName }) | Select -ExpandProperty NetworkAdapters).ipaddresses[0] | ForEach-Object {([system.net.dns]::GetHostByAddress($_)).hostname }

Make sure your VM is discoverable in the network.

确保您的VM在网络中可被发现。

#1


0  

You do have integration components installed on the guest? If you don't, install those ASAP. After the IC are up to date, you can query the host info from VM's registry like so,

您确实在guest虚拟机上安装了集成组件吗?如果不这样做,请尽快安装。 IC是最新的,您可以从VM的注册表中查询主机信息,如下所示,

gp 'HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters'
HostName                           : hyperv100.contoso.local
HostingSystemEditionId             : 8
...
PhysicalHostName                   : hyperv100
PhysicalHostNameFullyQualified     : hyperv100.contoso.local
VirtualMachineName                 : guest101

(gp 'HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters').PhysicalHostName
hyperv100

(gp 'HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters').PhysicalHostNameFullyQualified
hyperv100.contoso.local

In addition, read an article that describes the IC registry keys.

此外,阅读描述IC注册表项的文章。

#2


0  

One way is to fetch the IP address of the VM and from that you can query for the hostname. I have used single command to find the hostname as:

一种方法是获取VM的IP地址,然后您可以查询主机名。我使用单个命令来查找主机名:

$VMName = "TestVMName"
$host_name = ((Get-VM | where { $_.Name -eq $VMName }) | Select -ExpandProperty NetworkAdapters).ipaddresses[0] | ForEach-Object {([system.net.dns]::GetHostByAddress($_)).hostname }

Make sure your VM is discoverable in the network.

确保您的VM在网络中可被发现。