VMware Cloud Community
itayms
Contributor
Contributor
Jump to solution

snapshot delete

Hi,

i got a script that remove all the snapshots that are older then 14 days.

i want to change him and add this option:

if snapshot name contain the word consolidate or VADP , delete him after 1 day if the name is different delete after 14 days.

this is ny script:

Function SendMail

{

    param($body)

    $emailfrom = "Snapshot_Manager@blabla.co.il"

    $emailto ="IT-VM@blabla.co.il"

    $subject = "Snapshot Report"

    $smtpserver="smtpvs.blabla.co.il"

    $smtp = new-object net.mail.smtpclient($smtpserver)

    $smtp.Send($emailfrom,$emailto,$subject,$body)

}

$vCenter = $args[0]

if (($args[0]) -and ($args[1]))

{

connect-viserver $vCenter

$body="The Following Snapshots were Deleted:"

$body+=[environment]::NewLine

$snaps= get-vm  | get-snapshot

$now = date

foreach ($s in $snaps)

{

  $age = $now -$s.created

  if ($age.days -ge 14)

  {

  $VMname = get-vm -Id $s.VMId | select name

  $name = $VMname.name

  $snapname = $s.name

  $daysold= $age.days

  if ($args[1] -eq "-f")

  {

  remove-snapshot $s -confirm:$false

  }

  $body += "VM: $name Snap Name: $snapname Age: $daysold Days"

  $body+=[environment]::NewLine

  }

}

SendMail $body

disconnect-viserver -confirm:$false

}

else

{

  Write-host -foregroundcolor Red "usage:.\script [vCenter] <-f (force)>"

}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Change this line

if ($age.days -ge 14)

into

if (($age.days -ge 14 -and $s.Name -notmatch "consolidate|VADP") -or (($age.days -ge 1 -and $s.Name -match "consolidate|VADP")))


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

View solution in original post

0 Kudos
1 Reply
LucD
Leadership
Leadership
Jump to solution

Change this line

if ($age.days -ge 14)

into

if (($age.days -ge 14 -and $s.Name -notmatch "consolidate|VADP") -or (($age.days -ge 1 -and $s.Name -match "consolidate|VADP")))


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

0 Kudos