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

i thought you ask to verify by " displaying the contents of $global:defaultviservers? "

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That was the question mark for my question, no you shouldn't type the question mark, just $global:defaultviservers


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

0 Kudos
klee222
Enthusiast
Enthusiast
Jump to solution

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, going back to the error, you seem to be doing a Disconnect-VIServer from a server TestVC, which is not the name of your vCenter.

It looks as if the code you copied contains that TestVC somewhere in there.

Replace that with your own vCenter name.


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

0 Kudos
klee222
Enthusiast
Enthusiast
Jump to solution

Hi LucD

this is the screen shot , nothing came out ?

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Then you probably have no orphaned VMs


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

0 Kudos
klee222
Enthusiast
Enthusiast
Jump to solution

Hi lucD

i seem, but when i use your script it show some of this file ( i filtered by vmx)

pastedImage_0.png

any different ?? just curios

without filters is as per attachment  (sheet 1 is using your script, sheet 2 is using RV tools scan).

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you attach (as files) both scripts you are using?


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

0 Kudos
klee222
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

I updated with attached the result in excel sheet

(Apologize, i attached the correct document here again, left size column is with RV tools, right side is with your script)

0 Kudos
LucD
Leadership
Leadership
Jump to solution

What I meant was the code of both scripts, the one that doesn't return anything, and then the version of my code you are using.

That way I can compare which of the two might go wrong.


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

0 Kudos
klee222
Enthusiast
Enthusiast
Jump to solution

Hi LucD

here is the attachment.

from your script i copied and past into CLI, and then follow by command "Remove-OrphanedData -Datastore DS1" where DS1 i replace with my existing datastore one by one.

from another script i copied and past the "get-ProbablyOrphanedFile" , and then run  this  command "Get-Datastore | where {$_.name -like “SSD*”} | get-ProbablyOrphanedFile -matchpattern “*.vmx”| ogv" and follow by next script "DIsconnect-VIServer -Server “testVC” -confirm:$False" as per screen shot.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

With the 2nd script you are using a Where-clause to only look at datastores whose name starts with SSD.

Do you actually have such datastores?
Try leaving out the Where-clause, and only select DS1, like

Get-Datastore -Name DS1 | Get-ProbablyOrphanedFile -matchpattern “*.vmx”|

Out-GridView


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

0 Kudos
klee222
Enthusiast
Enthusiast
Jump to solution

Hi LucD

here is my datastore summary

pastedImage_0.png

and this is what i run just now

pastedImage_2.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You have to dot-source that Get-ProbablyOrphanedFile function before calling it.

Otherwise you get exactly that error.

Store the code of the function in a .ps1 file, then dot-source the .ps1 file from the prompt (. .\thefile.ps1, and yes, there is a blank between the two dots)

After that you should be able to call the function from the prompt.

From your datastore list, it looks like you have no datastores whose name starts with SSD.

So that explains why nothing comes out of the call you made earlier.


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

0 Kudos
klee222
Enthusiast
Enthusiast
Jump to solution

Sorry LucD

not sure which area went wrong ( I have test blank between two dot)

pastedImage_1.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The function Get-FileInDatastireWithWorkflow is missing.


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

0 Kudos
klee222
Enthusiast
Enthusiast
Jump to solution

Hi LucD

how to fix this? where edit ?

help help

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You'll find that missing function at the same location where you found the other function.

See Find orphaned vmdk files via workflow


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

0 Kudos
klee222
Enthusiast
Enthusiast
Jump to solution

Hi LucD

i'm not good to modify the function, can help ? apologize

also can i modify SSD to non-SSD

Get-Datastore | where {$_.name -like “SSD*”} | get-ProbablyOrphanedFile -matchpattern “*.vmx”| ogv

Get-Datastore | where {$_.name -like “NON-SSD*”} | get-ProbablyOrphanedFile -matchpattern “*.vmx”| ogv

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You will have to include all functions from that webpage in the .ps1 file that you dot-source.

The Where-clause you are using only looks at the name of the datastore, not the type of HW (SSD or non-SSD).


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

0 Kudos