Skip to content

Commit a01476a

Browse files
committed
Added better handling of no snapshots to delete.
1 parent bb14b15 commit a01476a

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

CloudStackSnapshotHistoryManager.ps1

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,26 @@ if ($parameters -ne 1) {
3333
$cloud = New-CloudStack -apiEndpoint $parameters[0] -apiPublicKey $parameters[1] -apiSecretKey $parameters[2]
3434
$job = Get-CloudStack -cloudStack $cloud -command listSnapshots -options volumeid=$volume
3535

36-
$snapshots = $job.listsnapshotsresponse.snapshot
37-
$days = 0 - $days
36+
if($snapshots = $job.listsnapshotsresponse.snapshot){
37+
$days = 0 - $days
3838

39-
$purgeDate = [DateTime]::Today.AddDays($days).DayOfYear
39+
$purgeDate = [DateTime]::Today.AddDays($days).DayOfYear
4040

41-
foreach($snap in $snapshots){
42-
Write-Debug $snap
43-
if ([DateTime]::Parse($snap.created).DayOfYear -lt $purgeDate){
44-
$snapDate = [DateTime]::Parse($snap.created).ToShortDateString()
45-
$snapID = $snap.id
46-
Write-Host "Deleting Snapshot $snapID from $snapDate."
47-
$deleteJob = Get-CloudStack -cloudStack $cloud -command deleteSnapshot -options id=$snapID
48-
Write-Debug "Delete job: $deleteJob"
49-
}
50-
51-
}
41+
foreach($snap in $snapshots){
42+
Write-Debug $snap
43+
if ([DateTime]::Parse($snap.created).DayOfYear -lt $purgeDate){
44+
$snapDate = [DateTime]::Parse($snap.created).ToShortDateString()
45+
$snapID = $snap.id
46+
Write-Host "Deleting Snapshot $snapID from $snapDate."
47+
$deleteJob = Get-CloudStack -cloudStack $cloud -command deleteSnapshot -options id=$snapID
48+
Write-Debug "Delete job: $deleteJob"
49+
}
50+
51+
}
52+
}
53+
else{
54+
Write-Host "No snapshots for volume $volume to delete."
55+
}
5256
}
5357
else {
5458
Write-Error "Please configure the $env:userprofile\cloud-settings.txt file"

0 commit comments

Comments
 (0)