VMware Cloud Community
a2alpha
Expert
Expert
Jump to solution

Get the name of the host a virtual machine is running on, rather than Host ID

Hi,

Looking for a bit of help, I am querying the result of a get-vm from the .Runtime to find the name of the host a Virtual Machine is running on. All I seem to get is the host id which is a number.

Is there an option from the get-vm that would display the name of the host or is there a way to pull out the name of the host from the host id value supplied.

What I would like to acheive is as follows:

Enter a VM name, get the host its running on, query the host for its available port groups, present an option of these port groups and upon input of the name, set the port group entered. Without the actual host name I can't seem to get this.

$selectedVM = read-host "Enter VM Name"

$vm = get-vm $selectedVM | get-view

$runhost = $vm.runtime.host

$portgroups = get-virtualportgroup -VMHost $runhost

"Available Port Groups to connect"

$portgroups

$selectedPG = read-host "Enter port group name"

$netadapters = get-networkadapter -VM $selectedVM

ForEach($netadapter in $netadapters)

{

set-networkadapter -NetworkAdapter $netadapter -NetworkName $selectedPG -StartConnected:$TRUE -confirm:$false

}

Any help on this would be really appreciated.

Thanks,

Dan

0 Kudos
1 Solution

Accepted Solutions
jeveenj
Enthusiast
Enthusiast
Jump to solution

Hi Dan,

You can fetch the host name using Get-VMHost cmdlet

$selectedVM = read-host "Enter VM Name"
$vmhost = Get-VMHost -VM (Get-VM -Name $selectedVM) | Select-Object Name
$runhost = $vmhost.Name

Hope this helps

-If you found this information useful, please consider awarding points for Correct or Helpful.

View solution in original post

0 Kudos
3 Replies
jeveenj
Enthusiast
Enthusiast
Jump to solution

Hi Dan,

You can fetch the host name using Get-VMHost cmdlet

$selectedVM = read-host "Enter VM Name"
$vmhost = Get-VMHost -VM (Get-VM -Name $selectedVM) | Select-Object Name
$runhost = $vmhost.Name

Hope this helps

-If you found this information useful, please consider awarding points for Correct or Helpful.
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You only have to adapt your script slightly.

The Get-VIObjectByVIView cmdlet will convert the Id to a PowerCLI VMHost object.

$selectedVM = read-host "Enter VM Name"
$vm = get-vm $selectedVM | get-view
$runhost = Get-VIObjectByVIView $vm.runtime.host
$portgroups = get-virtualportgroup -VMHost $runhost
"Available Port Groups to connect"
$portgroups
$selectedPG = read-host "Enter port group name"
$netadapters = get-networkadapter -VM $selectedVM
foreach($netadapter in $netadapters)
{
	set-networkadapter -NetworkAdapter $netadapter -NetworkName $selectedPG -StartConnected:$TRUE -confirm:$false
}

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

a2alpha
Expert
Expert
Jump to solution

Thank you both for your answers, both work brilliantly, I just wish that there was two correct answer buttons!

LucD - you have opened up a whole new concept for me with Get-VIObjectByVIView

Thanks again.

0 Kudos