Hi Team,
I have a script to identify vm which having snapshot more than one day as per below:
$vm = Get-VM -Name
$snaps = Get-Snapshot -VM $vm | where { $_.Created -lt (Get-Date).AddDays(-1) }
if ($snaps) {
Write-Output "Snapshot detected"
$snaps | Select-Object -Property @{N = 'VM'; E = { $_.VM.Name } },
Name, Description, Created | Format-List
} else {
Write-Output "NO snapshot"
'' | Select-Object -Property @{N = 'VM'; E = {$vm.Name} },
@{N='Name';E={'No snapshot'}},@{N='Description';E={'na'}},@{N='Created';E={'na'}} |
Format-List
}
____________________________
Secondly identify the snapshot Size as per below:
function Get-SnapshotInfo
{
param(
[VMware.Vim.VirtualMachineSnapshotTree]$Tree
)
$snapInfo = Get-View -Id $Tree.Snapshot
$vm = Get-View -Id $Tree.VM
$entry = $vm.LayoutEx.Snapshot | where{$_.Key -eq $Tree.Snapshot}
$files = $vm.LayoutEx.File | where{($entry.Disk | %{$_.Chain[-1]}).FileKey -contains $_.Key}
New-Object -TypeName PSObject -Property @{
Name = $Tree.Name
Created = $Tree.CreateTime
SizeGB = [math]::Round(($files | Measure-Object -Property Size -Sum).Sum/1GB,2)
}
if($Tree.ChildSnapshotList){
$Tree.ChildSnapshotList | ForEach-Object -Process {
Get-SnapshotInfo -Tree $_
}
}
}
$vm = Get-VM -Name
$vm.ExtensionData.Snapshot.RootSnapshotList |
ForEach-Object -Process {
Get-SnapshotInfo -Tree $_
}
____________________________
How may i combine both individual script into one.
I need the Result to be display with below condition:
1)Snapshot detected / No Snapshot detected
2)List of VM Name detected with more than 1 day snapshot, along with the snapshot size