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:
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
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.
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.
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.
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> |
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
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
Hah, perfect LucD thanks 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!