VMware Cloud Community
MHegelund
Contributor
Contributor
Jump to solution

List VMs in datastore that is not in inventory

Hi Guys

PowerCLI rookie here, so sorry for stupid questions.

I’m trying to clean up a bunch of singehost with local datastores. So I need a script that can output a list of virtual machine files on a datastore that is not being used by VMs in the inventory. One of the problem is that the files on the datastore, is not all ways named exactly the same as the VM’s in the inventory.

Been looking at this, but I think the term “orphaned” is ‘sent what I think it is:

https://communities.vmware.com/thread/266913

Also there is this one, which I think I should be able to modify to do what I want:

http://www.wooditwork.com/2011/08/11/adding-vmx-files-to-vcenter-inventory-with-powercli-gets-even-e...

Any hints or clues to push me in the right direction would be appreciated.

62 Replies
klee222
Enthusiast
Enthusiast
Jump to solution

Hi LucD

so i have to run these all script into CLI ?

Get-FilesIdentifiedAsAssociatedToAllVMs, Get-FileInDatastore,get-FileInDatastoreWithWorkflow & get-ProbablyOrphanedFile

Means copy each of the script and paste into CLI ? then only can work?

Also for SSD or non-ssd, i just have to enter the datastore name rather then SSD/Non-SSD.?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can paste the code on the prompt, but a better way would be to place all the functions n a .ps1 file.

And then dot-source this file. That way the functions are known to your PowerShell session.

Now you can make the call of the function.

Yes, the Where-clause you are using uses the datastorename.


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

Reply
0 Kudos
ccalvetTCC
Enthusiast
Enthusiast
Jump to solution

Luc Dekens has the right answer as always.

You need first the others functions. (I like the copy paste approach, even if it is not the best)

Keep in mind that the function get-ProbablyOrphanedFile  is actually quite complex with a mix of API call, PowerShell workflow, call of others function and there are no errors handling.

Moreover, it was developed a long time ago, and I have not tested it with new version. It should still work if the API have not been modified but this is not guaranteed.

It is suitable for experienced PowerCLI user that will have the skills to adapt and troubleshoot it as needed.

For your scenario, I will recommend a step by step troubleshooting.

Step 1: Get the correct list of datastores

Get-datastore | where {$_.something -eq “something”}

I do not think that there is a “Drive Type” property for a PowerCLI datastore object, so it is not possible to use something like $_.DriveType -eq "SSD".

Online Documentation - Cmdlet Reference - VMware {code}

However, I think the information that you are looking for is the “ssd” property of the API object “Host.VmfsVolume”

https://vdc-repo.vmware.com/vmwb-repository/dcr-public/fa5d1ee7-fad5-4ebf-b150-bdcef1d38d35/a5e46da1...

It is possible to “jump” from a powershell Datastore to an API datastore via “.extensiondata” property.

For more details about the core concepts of PowerCLI:

http://thecrazyconsultant.com/powercli-study-guide-core-concepts/

(This guide is not up to date but the majority of concepts still apply today)

Step 2: Try the function “Get-FileInDatastore” alone with only one datastore and your pattern

Get-datastore “Datastorename” | Get-FileInDatastore –matchPattern “YourPattern”

The goal is to check if the pattern is correct. (I am not sure if the pattern “*.vmx”| ogv" works.)

Actually, the format expected for this “pattern” is not well documented in the API.

https://code.vmware.com/doc/preview?id=4206#/doc/vim.host.DatastoreBrowser.SearchSpec.html

Step 3: Try the function “get-FileInDatastoreWithWorkflow” alone

Step 4: Try the function “Get-FilesIdentifiedAsAssociatedToAllVMs” alone

Step 5: Try the function "get-ProbablyOrphanedFile"

Blog: http://thecrazyconsultant.com/ | Twitter: @ccalvetTCC
Reply
0 Kudos