Shabat Closer

Thursday, May 22, 2014

TECH Windows Server 2008 - Power Shell: Auto delete old Shadow copies - script

TECH Windows Server 2K8 - Power Shell: Auto delete old Shadow copies - script

this script keep only the follwing  shadow copies
last 7 days : Daly at 12 A.M
after 7 days : only the days  Sunday and Wednesday for each week. (at 12 A.M)



$vss=vssadmin List Shadows
$vssContents=($vss,"" -split("Contents of shadow copy"))
foreach ($vssContent in $vssContents){
  if ($vssContent.length -gt 620 ){
  $ContentId=($vssContent,"" -split("Shadow Copy ID: {"))[1]
  $ContentId=($ContentId,"" -split("}"))[0]
  
  $CreationTime=($vssContent,"" -split("time: "))[1]
  $CreationTime=($CreationTime,"" -split("  "))[0]

  $CreationTime=[datetime]::Parse($CreationTime)
  $Diff=(New-TimeSpan $($CreationTime) $(Get-Date)).Days

  if  ($Diff-ge 1)
   if (($CreationTime.Hour -ne 0))
    cmd /c "vssadmin Delete Shadows /shadow={$ContentId} /Quiet"

  if  ($Diff-gt 5) 
   if (($CreationTime.DayOfWeek -ne "Sunday") -and ($CreationTime.DayOfWeek -ne "Wednesday"))
    cmd /c "vssadmin Delete Shadows /shadow={$ContentId} /Quiet"
 }
}
exit

No comments:

Post a Comment