Some of the values does not come up straight .. eg
(get-vm -name name1).extensiondata.runtime.host
Type
----
HostSystem
In above case how do we get the value of the VM's host ?
thanks
There is, like this
Get-View -ViewType VirtualMachine -Property Name,"runtime.host" |
Select @{N="VM";E={$_.Name}},@{N="ESXi";E={Get-view -Id $_.runtime.host | select -ExpandProperty Name}}
And if you want a specific VM, you can do
$vmName = "MyVM"
Get-View -ViewType VirtualMachine -Property Name,"runtime.host" -Filter @{"Name"=$vmName} |
Select @{N="VM";E={$_.Name}},@{N="ESXi";E={Get-view -Id $_.runtime.host | select -ExpandProperty Name}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
My assumption is that you are trying to retrieve the host name to which the VM resides, you could achieve this by invoking the Get-VM cmdlet as below:
(Get-VM name1).VMHost.Name
From your example, the value that will be retrieved will be the Host Id value and not the name, but you could return the value as below:
((Get-VM -Name name1).Extensiondata.Runtime.Host).Value
What Dean said is correct, but to clarify a bit further.
The ExtensionData property of PowerCLI objects, aka .Net objects, maps to the read-only copy of the corresponding vSphere object.
In this case for a .Net VirtualMachine object, the ExtensionData property will map to a vSphere VirtualMachine object.
Although the objects are named the same, they are 2 different objects.
To see what properties are in the vSphere object, you consult the API Reference.
If you follow the path to Host property you are using, you will see that the property holds a MoRef (which is a pointer to another vSphere object).
To get the vSphere object the MoRef is pointing, you can use the Get-View cmdlet.
Get-View -Id (Get-VM -Name MyVM).ExtensionData.Runtime.Host
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
((Get-VM -Name name1).Extensiondata.Runtime.Host).Value
did not give the expected output tho
What value are you expecting?
Yes, I understand that, but given the first answer would have ultimately been the quickest way, I was responding to the very last comment.
Thanks,
Get-View -Id (Get-VM -Name MyVM).ExtensionData.Runtime.Host
does revile the name of the vm's host, but there is no easy way to get this with " get-view -ViewType VirtualMachine"
Then cluster too.
There is, like this
Get-View -ViewType VirtualMachine -Property Name,"runtime.host" |
Select @{N="VM";E={$_.Name}},@{N="ESXi";E={Get-view -Id $_.runtime.host | select -ExpandProperty Name}}
And if you want a specific VM, you can do
$vmName = "MyVM"
Get-View -ViewType VirtualMachine -Property Name,"runtime.host" -Filter @{"Name"=$vmName} |
Select @{N="VM";E={$_.Name}},@{N="ESXi";E={Get-view -Id $_.runtime.host | select -ExpandProperty Name}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks that did work... But the purpose of using get-view does not meet as its taking a lot of time with this added change ..
i think its even slower than , get-vm |selec name, vmhost ..
Very strange, how many VMs do you have in that environment ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Here is the o/p
PowerCLI C:\> Measure-Command {get-vm |select name,vmhost}
Days : 0
Hours : 0
Minutes : 0
Seconds : 32
Milliseconds : 791
Ticks : 327911132
TotalDays : 0.000379526773148148
TotalHours : 0.00910864255555555
TotalMinutes : 0.546518553333333
TotalSeconds : 32.7911132
TotalMilliseconds : 32791.1132
PowerCLI C:\> Measure-Command {Get-View -ViewType VirtualMachine -Property Name,"runtime.host" |Select @{N="VM";E={$_.Name}},@{N="ESXi";E={Get-view -Id $_.runtime.host | select -ExpandProperty Name}} }
The last cmd will give o/p may be after 30 min or 1 hr is my estimate .. but it gives info correct ..
btw i have 1500 vms
also ran it for single vm
PowerCLI C:\> Measure-Command {get-vm xxxxxxxx |select name,vmhost}
Days : 0
Hours : 0
Minutes : 0
Seconds : 8
Milliseconds : 191
Ticks : 81913350
TotalDays : 9.48071180555556E-05
TotalHours : 0.00227537083333333
TotalMinutes : 0.13652225
TotalSeconds : 8.191335
TotalMilliseconds : 8191.335
PowerCLI C:\> $vmName = "xxxxxxxx"
PowerCLI C:\>
PowerCLI C:\> Measure-Command {Get-View -ViewType VirtualMachine -Property Name,"runtime.host" -Filter @{"Name"=$vmName} |Select @{N="VM";E={$_.Name}},@{N="ESXi";E={Get-view -Id $_.runtime.host | select -ExpandProperty Name}} }
Days : 0
Hours : 0
Minutes : 0
Seconds : 11
Milliseconds : 540
Ticks : 115409488
TotalDays : 0.000133575796296296
TotalHours : 0.00320581911111111
TotalMinutes : 0.192349146666667
TotalSeconds : 11.5409488
TotalMilliseconds : 11540.9488
PowerCLI C:\>
The Get-View variation is doing a lot more of course, compared to the Get-VM with the Select-Object.
In this case you should go for the simple Get-VM.
But the original question was why the runtime.host property didn't give the ESXi hostname.
The short answer, because it returns a MoRef.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference