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

i'm new here and i dont get it how to run those script.

i copy entired script from Orphaned files and folders - Spring cleaning - LucD notes

and pass to power cli. nothing came out?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That script is a function, it will not do anything unless you call the function.

See the Sample Runs sections on how to call the function.

Place everything, function & function call, in a .ps1 file.

Then "run" the .ps1 file from your PowerShell prompt.


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

0 Kudos
klee222
Enthusiast
Enthusiast
Jump to solution

Hi LucD

sorry to ask but how to "Place everything, function & function call, in a .ps1 file." ?

I'm trying to get list and export to csv for those vm that are not presented / added to inventory but in datastore, so i found this article

how should i edit and run it correctly ?

thanks you

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Pick an editor (can be anything: notepad, notepad ++, PS ISE editor, VS Code....).
Open a new file.Copy the function in that file.
Then copy the call of the function into that file below the function.

Save the file, pack any name but use the filetype ps1.

From the PS prompt, call the .ps1 file by typing the path to it. For example C:\Scripts\MyScript.ps1

If this is your first use of PowerShell, you will probably have to set the PowerShell ExecutionPolicy first.

Do a 'Set-ExecutionPolicy -ExecutionPolicy RemoteSigned' from the PS prompt (make sure to start PowerShell as an Admin).

There are a number of decent PowerShell introduction articles out there.
You should start by reading one of those.
See for example Running PowerShell Scripts Is as Easy as 1-2-3


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

0 Kudos
klee222
Enthusiast
Enthusiast
Jump to solution

Hi LucD.

Haha...i think i getting lose

1.     i copy the script from 1st line

function Remove-OrphanedData {

<#

.SYNOPSIS   Remove orphaned folders and VMDK files

until last

127 | }

2. save this file in ps, example "orphaned.ps1" in some where like c:\script

3. run power CLI and connect to Vcenter

4. in power CLI, open this file from where it was save

5. type "Remove-OrphanedData" , enter

and result came out

correct ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You forgot one part, you have to call the function.
At the bottom of the file add one like you find the Sample Runs section of the blogpost.


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

0 Kudos
klee222
Enthusiast
Enthusiast
Jump to solution

Hi  LucD,

i'll try it out and let you know if any problem.

although i seldom came here or only came here with some problem. i always saw your post any where LucD,

excellent

mean while i have a question on new post with subject "Setup my 1st complete Vcenter with 8 host, need advice. ( 8 host, 2 VC)"

hope you can guide me there

many many thanks

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That 2nd question is a quite general one, and is more related to designing a vSphere setup.

I'm not sure if you'll find an answer on that in these communities.


I agree with one of the responses in that thread, helping you with a design is more for a professional services person or a consultant or an experienced person on site.

They will have to get to know your requirements, learn about limitations, available HW, infra, and so on.

That is definitely not something you would get an answer on in a few lines in a community thread I'm afraid.


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

0 Kudos
klee222
Enthusiast
Enthusiast
Jump to solution

Hi LucD

i have save the file in PS and when i call it nothing happen?

pastedImage_0.png

what went wrong?

when i copy the file manually, here is the errorpastedImage_0.png

and another try

pastedImage_4.png

help help

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is not what I said, you have to add that line to the .ps1 file at the end, not enter it at the prompt


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

0 Kudos
klee222
Enthusiast
Enthusiast
Jump to solution

Hi LucD

apologize, now i get it.

but can i check with you these error ?

1)  Name not correct ?

pastedImage_4.png

pastedImage_3.png

2) i try manually copy script and pass into CLI , and then run the function call manually by enter datastore one by one. all seem pass?

also i notice some command just take mili second (superfast,feel like not working)

pastedImage_8.png

pastedImage_5.png

3) Data store not work in here? because not cluster?

pastedImage_6.png

pastedImage_9.png

sorry to take time understand the script

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you have special characters in the datastorename, enclose the name in single quotes.


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

0 Kudos
klee222
Enthusiast
Enthusiast
Jump to solution

Hi LucD

thanks for the guide, is there any way to run as the datastore are in use and there is VM inside.

Not sure what happen if renamed it. ><'

for question 2 and 3 would you help verify, sorry for asking so much .

thanks you

0 Kudos
LucD
Leadership
Leadership
Jump to solution

2. If there are no, or hardly any VMs on that datastore, the function might return quite quickly

But are these perhaps NFS datastores?
The default function only looks at VMFS datastores

3. Shouldn't make a difference

Not sure what you mean with "...to run as the datastore are in use and..."

The idea is that this function runs against datastores that are used to store VMs


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

0 Kudos
klee222
Enthusiast
Enthusiast
Jump to solution

Hi LucD

missing grammar

is there anyway to run same as the name given to datastore, it was in use and there is vm insdie

Not sure what happen if renamed it. ><'

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Still not sure what you mean I'm afraid.

If you renamed the datastore, then you use the new datastorename.

If you renamed a VM, and unregister it, it should be found by the script


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

0 Kudos
klee222
Enthusiast
Enthusiast
Jump to solution

Hi

I Went to the link

http://thecrazyconsultant.com/find-orphaned-vmdk-files-workflow/

and copy the last script  - get-ProbablyOrphanedFile and past it to CLI.

then copy the script as following , any idea why the result was not show?pastedImage_4.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you sure you where connected?

After the Connect-VIServer did you check by displaying the contents of $global:defaultviservers?


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

0 Kudos
klee222
Enthusiast
Enthusiast
Jump to solution

Hi LucD

not sure if this are correct ?

pastedImage_0.png

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Connect-VIServer seems to be correct.

Not sure why you have the '?' after $global:defaultviservers


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

0 Kudos