Requirement
Install VMWare PowerCLI
Run the following command in elevated PowerShell
# The line below will install VMWare PowerCLI on current user environment
# Steps below will require internet connection to download the module from online repository
Install-Module -Name VMware.PowerCLI -Scope CurrentUser
Custom PowerShell module
Copy the following into a file located at Documents\WindowsPowerShell\Modules\VM.Statistics\VM.Statistics.psm1.
function Get-VMSnapshotSummary{
<#
.SYNOPSIS
Retrieves a summary of VM snapshots
.EXAMPLE
PS> Get-VMSnapshotSummary -VCenterServer vcenter.example.com | Select Name, Folder
This example retrieves VM snapshots summary managed by vCenter
.DESCRIPTION
Version History
------------------------------------------------------------------------------------------
Ver Author Date Remarks
01 Ben 22 Jul 2019 Initial Release with minor adjustment
------------------------------------------------------------------------------------------
.LINK
.PARAMETER VCenterServer
Put vCenter address here
#>
[CmdletBinding()]
param (
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$VCenterServer = "vcenter.company.com"
)
process
{
try
{
if(Get-Module -ListAvailable -Name VMware.VimAutomation.Core){
#Connection to VCenter
Connect-VIServer $VCenterServer | Out-Null
Get-VM | Get-Snapshot | Select VM,
Name,
@{Name="SizeGB";Expression={ [math]::Round($_.SizeGB,2) }},
@{Name="Age";Expression={ (New-TimeSpan -End (Get-Date) -Start $_.Created).Days }}
Disconnect-VIServer -Server $VCenterServer -confirm:$false | Out-Null
}
else{
Write-Host "VMware.VimAutomation.Core module does not exist. Steps to install:"
Write-Host "1. Open Powershell in Elevated mode"
Write-Host "2. Run 'Save-Module -Name VMware.PowerCLI -Path C:\Temp\PowerCLI'"
Write-Host "3. Run 'Install-Module -Name VMware.PowerCLI'"
}
}
catch
{
Write-Error -Message "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)"
}
}
}
Export-ModuleMember -function Get-VMSnapshotSummary
Query Statistics
On PowerShell, run the following command
# Make sure requirement above is completed before running script below
Get-VMSnapshotSummary -VCenterServer vcenter.example.com
Running user (PowerShell client) will need to have connect and read permission from VCenter.