VMware Cloud Community
Shoganator
Enthusiast
Enthusiast
Jump to solution

How do I find the name of a VM from ExtensionData.VM?

Hi guys,

I am trying to determine the VM name using Get-Datastore cmdlet and ExtensionData.VM

If I do:

$test = Get-Datastore SharedDatastore1

$test.ExtensionData.Vm

I'll get an output like this:

Type                                         Value
----                                         -----
VirtualMachine                               vm-208
VirtualMachine                               vm-212
VirtualMachine                               vm-213
VirtualMachine                               vm-214
VirtualMachine                               vm-301

Now clearly those are the actual VMs running on this datastore. What I'd like to do, is somehow convert those Values "vm-208" etc... into something more meaningful - i.e. the VM name. Can anyone offer any advice as to how to achieve this?

Thanks,

Sean

My personal blog: http://www.shogan.co.uk .::. Twitter: shogan85 .::. if an answer has helped or solved your question, please don't forget to mark as "Answered" or "Helpful"!
0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

Hello, Shoganator-

Those values returned are ManagedObjectReferences of the VMs on the given datastore.  You can use Get-VIew to the the .NET View objects of the given VMs using the -Id parameter, like so:

Get-View -Id (Get-Datastore SharedDatastore1).ExtensionData.Vm -Property Name | Select name

Since the -Id paramter takes either a single ManagedObjectReference or an array thereof, there is no need to do a Foreach-Object, you can just pass the entire array of MORefs.  I also used the -Property parameter so as to only return the desired property for the VM Views, which greatly increases speed and redeuces memory usage with the PowerShell session (important as scripts get larger/more complex).  Enjoy.

View solution in original post

0 Kudos
6 Replies
mattboren
Expert
Expert
Jump to solution

Hello, Shoganator-

Those values returned are ManagedObjectReferences of the VMs on the given datastore.  You can use Get-VIew to the the .NET View objects of the given VMs using the -Id parameter, like so:

Get-View -Id (Get-Datastore SharedDatastore1).ExtensionData.Vm -Property Name | Select name

Since the -Id paramter takes either a single ManagedObjectReference or an array thereof, there is no need to do a Foreach-Object, you can just pass the entire array of MORefs.  I also used the -Property parameter so as to only return the desired property for the VM Views, which greatly increases speed and redeuces memory usage with the PowerShell session (important as scripts get larger/more complex).  Enjoy.

0 Kudos
Shoganator
Enthusiast
Enthusiast
Jump to solution

Matt, simply brilliant. Thank you - by removing the -Property part I can see how the cmdlet then returns much more (unecessary in this case) info and how specifying to grab only the Name property helps speed and memory usage.

I can see great things ahead now that I am able to view this info.

My personal blog: http://www.shogan.co.uk .::. Twitter: shogan85 .::. if an answer has helped or solved your question, please don't forget to mark as "Answered" or "Helpful"!
0 Kudos
Shoganator
Enthusiast
Enthusiast
Jump to solution

Matt, quick question about the results of the Get-View cmdlet - when output to HTML using ConvertTo-HTML, I get an asterisk (*) in the top <TH> tag. Is there any way to change this to a string of my choice, or modify this? Also, what is the reason for this asterisk? Example output:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>HTML TABLE</title>

</head><body>

<table>

<colgroup>

<col/>

</colgroup>

<tr><th>*</th></tr>

<tr><td>SEAN-IMPORTER</td></tr>

<tr><td>FreeNAS-test2</td></tr>

<tr><td>FreeNAS-test1</td></tr>

<tr><td>FeeNAS-test2-clone</td></tr>

<tr><td>64bit-test</td></tr>

</table>

</body></html>

My personal blog: http://www.shogan.co.uk .::. Twitter: shogan85 .::. if an answer has helped or solved your question, please don't forget to mark as "Answered" or "Helpful"!
0 Kudos
Shoganator
Enthusiast
Enthusiast
Jump to solution

Ok I figured out how to get rid of the asterisk. Basically I had to use

| Select-Object " ", Name

So my original script that was generating the above HTML with the asterisk was:

Get-View -Id (Get-Datastore SharedDatastore1).ExtensionData.Vm -Property Name | Select name | ConvertTo-HTML | out-file c:\aaa.html

Then the fixed script is:

Get-View -Id (Get-Datastore SharedDatastore1).ExtensionData.Vm -Property Name | Select name | Select-Object " ", Name | ConvertTo-HTML | out-file c:\aaa.html

I am still not quite sure why I need to use Select-Object with a blank string, so if anyone could explain that part, that would be great - The problem I have now, is that there now two "properties" being selected, so I end up getting two columns generated with ConvertTo-HTML. One is blank of course, where I specified to select " ". Can anyone explain why the asterisk appears if I only select "Name"? Or any other work arounds? This is what I end up with now:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>HTML TABLE</title>

</head><body>

<table>

<colgroup>

<col/>

<col/>

</colgroup>

<tr><th> </th><th>Name</th></tr>

<tr><td></td><td>SEAN-IMPORTER</td></tr>

<tr><td></td><td>FreeNAS-test2</td></tr>

<tr><td></td><td>FreeNAS-test1</td></tr>

<tr><td></td><td>FeeNAS-test2-clone</td></tr>

<tr><td></td><td>64bit-test</td></tr>

</table>

</body></html>

Message was edited by: Shoganator

My personal blog: http://www.shogan.co.uk .::. Twitter: shogan85 .::. if an answer has helped or solved your question, please don't forget to mark as "Answered" or "Helpful"!
0 Kudos
LucD
Leadership
Leadership
Jump to solution

When your array you are converting to HTML only has 1 column, you always will get the asterisk  in the header.

The better solution is to use the -Property parameter on the ConvertTo-Html cmdlet.

Get-View -Id (Get-Datastore MyDS).ExtensionData.Vm -Property Name | Select Name | ConvertTo-Html -Property Name

No need to pass dummy properties.


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

Shoganator
Enthusiast
Enthusiast
Jump to solution

Hah, perfect LucD thanks Smiley Happy Just as I edited my post too! I knew it was down to ConvertToHTML, but after going through the the ref page for that cmdlet over and over, I just did not click that I should use Property! :smileyblush:

Thanks again!

My personal blog: http://www.shogan.co.uk .::. Twitter: shogan85 .::. if an answer has helped or solved your question, please don't forget to mark as "Answered" or "Helpful"!
0 Kudos