Powercli to Retrieve VM snapshots info from Datastore

Powercli to Retrieve VM snapshots info from Datastore

I need a Powercli command that retrieves the Snapshot of VMs not only the snapshots displayed on the Snapshot Manager , but also the ones that are not in the Snapshot Manager but they are still on the Datastore.

For now I need Powercli command to test it on a single VM , That I know it has snapshot on Datastore and that does not show in Snapshot Manager.

Thank you
jskfanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Andrew Hancock (VMware vExpert / EE MVE^2)VMware and Virtualization ConsultantCommented:
Powercli to Retrieve VM snapshots info from Datastore

A Powercli script is run against vCenter Server , and can only return snapshots which have been created or managed by vCenter Server.

These simple commands below will return snapshots

Get-VM | Get-Snapshot 

Open in new window

- but these returned are only if managed by vCenter Server, which I know you want a script which shows snapshots not managed by vCenter Server.

better formatted

get-vm | get-snapshot | Select-Object -Property vm,created,sizeGB,name,description

Open in new window


If the snapshot does not appear in vCenter Server it will not be returned by the script. e.g. these snapshots are usually created by direct access to the API and not via vCenter Server.

So the only method to check for ALL snapshots, is just a general script which lists the snapshot files on the datastores, which effectively is just a script which executes a bash command.

e.g.
ls -al

Open in new window


and to search all datastores

find /vmfs/volumes/ -iname „*delta.vmdk”

Open in new window


but the above are bash commands, not a script, but we can execute bash commands from a PowerCLI script - if this is of interest to you..

There is a complex Snapshot Summary report script here

https://code.vmware.com/forums/2530/vsphere-powercli#578931

You could also use and try the following and see if it works for you... Snap Watcher..

https://try.opvizor.com/snapwatcher/

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jskfanAuthor Commented:
This will retrieve the VMs that have snapshot in the Datastore but not displayed on Snapshot Manager:
Get-VM | Where-Object {$_.Extensiondata.Runtime.ConsolidationNeeded}


This will retrieve VMs that have Snapshots in the Snapshot  Manager. If it is not in the Snapshot Manager, but it is in the Datastore  , it will not be retrieved.
get-vm | get-snapshot  | Select-Object -Property vm,created,sizeGB,name,description
Andrew Hancock (VMware vExpert / EE MVE^2)VMware and Virtualization ConsultantCommented:
This will retrieve the VMs that have snapshot in the Datastore but not displayed on Snapshot Manager:
Get-VM | Where-Object {$_.Extensiondata.Runtime.ConsolidationNeeded}

That checks if a consolidation is needed.

The problem here, is that Consolidation Warning is dumb! and the consolidation message/warning appears, because it's just a watcher/watchdog service for the presence of a snapshot file e.g. 0000x.vmdk on the datastore, it does not check if it's actually being used....

So you can get a Consolidation warning, and keep trying to "consolidate" and it never disappears.

This will retrieve VMs that have Snapshots in the Snapshot  Manager. If it is not in the Snapshot Manager, but it is in the Datastore  , it will not be retrieved.
get-vm | get-snapshot  | Select-Object -Property vm,created,sizeGB,name,description

Correct, that's what I posted.

RV Tools, does a very good job, and so does Snap Watcher..

Set an Alarm in vCenter Server or Veeam One.
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

jskfanAuthor Commented:
The problem here, is that Consolidation Warning is dumb! and the consolidation message/warning appears, because it's just a watcher/watchdog service for the presence of a snapshot file e.g. 0000x.vmdk on the datastore, it does not check if it's actually being used....


for now I just wanted to retrieve the existence of the snapshots...

So you can get a Consolidation warning, and keep trying to "consolidate" and it never disappears.

Vmware admin I talked to had the same issue , per his statement he had to "stop vmware services on the ESX host " then run the consolidation.

I do not know what he meant by stopping Vmware services..... is that he migrated all the VMs from that host to another host and run consolidation ?   would that make consolidation faster?
I have read online that cloning a VM will do the consolidation in an efficient way.

I know it is not Consolidation question...but as you see in Technology things can get too much involved
Andrew Hancock (VMware vExpert / EE MVE^2)VMware and Virtualization ConsultantCommented:
If you want to know the existence of Snapshots, set a vCenter Server ALARM and it will email, and flag the VM as Red Alert.

or invest in Snapwatcher.

or just find them at the ESXi console - find /vmfs/volumes/ -iname „*delta.vmdk”.

Later you will have to work out how to deal with them.

I'm afraid each and every snapshot issue is different, many different reasons why a snapshot occurred.

would that make consolidation faster?
- No not really.

I have read online that cloning a VM will do the consolidation in an efficient way.

Not sure about that either...

I know it is not Consolidation question...but as you see in Technology things can get too much involved

I could present a 1 day Conference, on Snapshots alone.... as to why they occur, how they work, and don't work, and how to fix.
Luciano PatrãoICT Senior Infraestructure  Engineer  Commented:
Hi,

This is my script that I use for find any snapshots older than (we set the minimum days of the snapshot to find), then have the option to delete them.

$vCenter = 'IP/FQDN'
$user = "user"
$pwd = "password"
#Connect-VIServer $vCenter 

Connect-VIServer $vCenter -User $user -Password $pwd

### vCenter Server Name(FQDN)
$vCentName = [system.net.dns]::GetHostByAddress($vCenter).hostname
################################################
cls

$VMs = Get-VM
#$VMs = Get-Cluster "Cluster" | Get-VM
foreach ($VM in $VMs)
      {
      $AllSnapshots= Get-Snapshot -VM $VM
            foreach ($Snapshot in $AllSnapshots)
                 {
                        #If ($Snapshot.Name -like “VEEAM BACKUP*” -and $Snapshot.Created -lt (Get-Date).AddDays(-1))
						If ($Snapshot.Created -lt (Get-Date).AddDays(-5))
                              {     
                              Write-Host "VM -> " $VM.Name, 
                              Write-Host "  Snapshot Name: " $Snapshot.Name
							  Write-Host "  Snapshot Data: " $Snapshot.Created
                              Write-Host "Remove Snapshot :" $Snapshot.Name -ForegroundColor Red
                             # Remove-Snapshot -snapshot $Snapshot -confirm:$false -ErrorAction SilentlyContinue
                              Write-Host ""
							  #Start-Sleep -s 30
							  
                              }
                  }
      }
Disconnect-VIServer -Server $vCenter -Force -Confirm:$false

Open in new window


You can connect to a vCenter or a ESXi host. In all Clusters or just to a specific Cluster (remove comment to user any of different options).
Also the delete snapshot is commented, first list all snapshots, after if you agree, run again with the remove enable to enable all listed snapshots.

I use this script also to find any Snapshot left behind by Veeam Backup (that option is also commented).

Hope this can help.
jskfanAuthor Commented:
** is the script above Powershell Script ? I mean do I need to save it as .PS1
**Dos that script list the Snapshots that are not displayed on Snapshot Manager ?

***I would like to give a try your Script, if you can remove anything that has to do with modification or deletion. I want just to list the snapshots that are older than X days
Luciano PatrãoICT Senior Infraestructure  Engineer  Commented:
If you run the script in a script editor, you dont need to save into a ps1 file.

I use PowerGUI editor.

If not, then yes you should run this through a ps1 file.

All extra commands are commented, so is save to tun the script. Only if you want to delete etc., you will need do remove the comment %
Andrew Hancock (VMware vExpert / EE MVE^2)VMware and Virtualization ConsultantCommented:
If you use PowerCLI to connect to vCenter Server, PowerCLI will display Snapshots Managed and maintained by vCenter Server.

The script posted still uses the command....

Get-Snapshot -VM $VM

Open in new window


so... see my first post....

**Dos that script list the Snapshots that are not displayed on Snapshot Manager ?

No.

If you want to list ALL snapshots on your data stores....

use find...
jskfanAuthor Commented:
Thank you
Andrew Hancock (VMware vExpert / EE MVE^2)VMware and Virtualization ConsultantCommented:
No problems, glad to assist you with your Questions. Have a good day.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Virtualization

From novice to tech pro — start learning today.