VMware Cloud Community
GrantBrunton
Enthusiast
Enthusiast

Script to check for Orphaned Files and Invalid Snapshots

Hi All

I have created a Powershell script that checks for Orphaned Files and Invalid Snapshot images.

The Orphaned Files part of the check is using Luk Dekens function for finding and removing phantom files available here.

The other part of the script checks for servers that are using snapshots that do not exist in snapshot manager or servers which have a Consolidated Helper snapshot.

We currently run this script daily and it sends us an email if it finds any problems.

Simply alter the variables in the top part of the script and it should be able to run in any environment that has PowerCLI installed.

14 Replies
LucD
Leadership
Leadership

Nice script, thanks for sharing.


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

Reply
0 Kudos
boardt
Contributor
Contributor

Sorry for being dumb.. but how do I modify the script to use my environment?

Which lines do I put in my datastore name(s), the "test run", etc?

Reply
0 Kudos
GrantBrunton
Enthusiast
Enthusiast

You only have to edit the variables in the top section of the script to specify the virtual centre server and who you want to email the report to.

By default it checks all datastores in vcenter.

The script doesn't perform any activity on the datastores or files it will just generate and email out a report on any issues it might find.

(well it can also delete orphaned files but only if you add the -Delete switch to the Check-OrphanedData command)

Reply
0 Kudos
Pinball
Enthusiast
Enthusiast

Hi Grant & Luc

Thanks for a great script. I do need some help with it please. I've modified the script variables but get the following when running with PowerCLi 5.5 R2 and VC 5.5 U1c

PowerCLI C:\PS> .\Get-Snap.ps1

Get-HardDisk : 04/09/2014 08:32:29    Get-HardDisk        System error.

At C:\PS\Get-Snap.ps1:300 char:19

+             $vm | Get-HardDisk | %{

+                   ~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-HardDisk], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.GetHardDisk

Get-HardDisk : 04/09/2014 08:32:38    Get-HardDisk        System error.

At C:\PS\Get-Snap.ps1:300 char:19

+             $vm | Get-HardDisk | %{

+                   ~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-HardDisk], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.GetHardDisk

Get-HardDisk : 04/09/2014 08:32:38    Get-HardDisk        System error.

At C:\PS\Get-Snap.ps1:300 char:19

+             $vm | Get-HardDisk | %{

+                   ~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-HardDisk], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.GetHardDisk

Any advise?

Thanks

Johan

Reply
0 Kudos
fedayn
Contributor
Contributor

Hi,

My PowerCLI output is the same tna your, but I got the report, nonetheless.

regards.

Reply
0 Kudos
LucD
Leadership
Leadership

It would be interesting to know for which VMs you get these errors.

Perhaps display the name of the VM on screen before the Get-Harddisk cmdlet is executed.

I suspect these VMs might have a funny configuration.


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

Reply
0 Kudos
BryanWard
Contributor
Contributor

You may need to consolidate the disks. See the following for more information: Re: Virtual Machine Disks consolidation is needed

Reply
0 Kudos
nimos001
Enthusiast
Enthusiast

Seeing errors/warnings with this script I assume because it's older..

vm.png

Reply
0 Kudos
LucD
Leadership
Leadership

I suspect you are connected to multiple vSphere servers in multiple mode (see Get-PowerCLIConfiguration)

As a result $dsBrowser will contain multiple object, and is in fact an array.

And the SearchDatastoreSubFolders method is not known on an array (that is in fact what the error is saying)


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

nimos001
Enthusiast
Enthusiast

You are correct I had logged into VC before executing the script which resulted in that outcome. Looks like its working now. This script will only show INVALID snapshots and not VALID snapshots correct?

Reply
0 Kudos
LucD
Leadership
Leadership

That is correct.

Like the author explained "The other part of the script checks for servers that are using snapshots that do not exist in snapshot manager or servers which have a Consolidated Helper snapshot."


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

Reply
0 Kudos
C_Perdereau
Contributor
Contributor

I got the same problem with the Get-HardDisk pipe, here is the fix:

Just replace these 2 lines

$vm | Get-HardDisk | %{

        if ($_.Filename -match ".*-[0-9]{6}.vmdk")

with

foreach ($hd in $vm | Get-HardDisk) {

        if ($hd.Filename -match ".*-[0-9]{6}.vmdk")

I believe something weird is happening in the object pipe with the original version

Reply
0 Kudos
AnashMatar
Contributor
Contributor

Hi Guys,

I need  to detect and report only without deletion, any idea?

Best Regards

Reply
0 Kudos
LucD
Leadership
Leadership

Afaik this script only reports and doesn't do any deletes


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

Reply
0 Kudos