pamiller21
Enthusiast
Enthusiast

Script to clean up datastores

Hey all,

I am looking for a script to to help me clean up datastores from left over files.  After vmotion sometimes the datastore leaves behind folders with only files with the exts, .hlog .nvram -aux.xml and .vmxf files.  I would like to see if it is possible to make a script to help round up and delete these.

Thanks in advance,

Andy

0 Kudos
27 Replies
LucD
Leadership
Leadership

To look at orphaned VMDK files you can have a look at my Orphaned files and folders – Spring cleaning post.


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

0 Kudos
pamiller21
Enthusiast
Enthusiast

I did review that earlier, but the problem is I am not getting orphaned vmdk files but orphaned files like I said in the original post.

0 Kudos
LucD
Leadership
Leadership

Working on an update of my function that does all files.


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

0 Kudos
pamiller21
Enthusiast
Enthusiast

Awesome, any timeline on that sorta change? Week, month, 6 month?

Thanks dude!

0 Kudos
LucD
Leadership
Leadership

How about now :smileygrin:

See Orphaned Files Revisited


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

0 Kudos
pamiller21
Enthusiast
Enthusiast

Wow you are awesome.  I got the function up and running and got it working, but does it have a delete switch I can use to delete the files it found?

0 Kudos
LucD
Leadership
Leadership

No, I consciously decided not to include a Delete switch.

There will be another function coming that you can use to create and delete files.

The idea is that the output of Get-VmwOrphan is piped to the to-be function


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

0 Kudos
pamiller21
Enthusiast
Enthusiast

OHHHH You tease me with a perfect clean up method :smileygrin: well any idea how long it will be until the next function is up and going? :smileygrin:

0 Kudos
pamiller21
Enthusiast
Enthusiast

Just curious if there was any update on this?

Thanks dude!

0 Kudos
LucD
Leadership
Leadership

Nearly there :smileyblush:


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

0 Kudos
pamiller21
Enthusiast
Enthusiast

*sits eating popcorn staring with anticipation*

Thank you I am so grateful for this!!

0 Kudos
LucD
Leadership
Leadership

Rofl :smileylaugh:


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

0 Kudos
pamiller21
Enthusiast
Enthusiast

Hey dude any news on this? I am just out of popcorn and need to know if it is a go or I should make some more.

0 Kudos
LucD
Leadership
Leadership

No Halloween candy left then?
Seriously, the function is there, but as you may have noticed from some recent tweets of mine, I discovered some files for which I don't know if they can be removed safely or not.


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

0 Kudos
pamiller21
Enthusiast
Enthusiast

Sorry I don't use twitter much at all.  I looked around your site and not seeing the function so it is still in the production with the potential threat of removing unsafe files?

0 Kudos
LucD
Leadership
Leadership

No problem.

It is taking quite long,

I'll make some changes to bypass the issue for now, and post that here.


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

0 Kudos
LucD
Leadership
Leadership

In the attached file you'll find two functions, a revised Get-VmwOrphan and the new Remove-VmwFile.

When you try out the Remove-VmwFile function I strongly suggest to first always add the WhatIf switch, that way you are sure the correct files are removed.

Like this

Get-Datastore -Name MyDS | Get-VmwOrphan | Remove-VmwFile -WhatIf


You can also do this in two steps..

Like this for example

Get-Datastore -Name MyDS | Get-VmwOrphan | Export-Csv -Path report-orphan.csv -NoTypeInformation -UseCulture

Import-Csv -Path report-orphan.csv -UseCulture | Remove-VmwFile -WhatIf

You can also combine both with the Tee-Object cmdlet.

Get-Datastore -Name MyDS | Get-VmwOrphan | Tee-Object -Variable buffer | Remove-VmwFile -WhatIf

$buffer | Export-Csv -Path report-orphan.csv -NoTypeInformation -UseCulture


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

0 Kudos
pamiller21
Enthusiast
Enthusiast

This is looking excellent! I just wanted to make sure I understand correctly as this is a touchy script.  If I run this line:

Get-Datastore -Name MyDS | Get-VmwOrphan | Remove-VmwFile -WhatIf


Nothing will get deleted, because of the whatif tag.  If I remove that tag, all will get deleted. And if I run your first example it should look like 2 lines, first use this line:

Get-Datastore -Name MyDS | Get-VmwOrphan | Export-Csv -Path report-orphan.csv -NoTypeInformation -UseCulture


Then if the output looks acceptable, then run this command:

Import-Csv -Path report-orphan.csv -UseCulture | Remove-VmwFile -WhatIf



Again a huge thanks!!

0 Kudos
LucD
Leadership
Leadership

Both your assumptions are correct.


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

0 Kudos