Sunday, 25 October 2015

Apply cDOT Volume Snapshot Policy PowerShell Function

Continuing from the previous post and applying volume comments...

Function Apply-CmSnapPol can be used to apply snapshot policy to a volume, and returns true if successful and false if not.

FUNCTION Apply-CmSnapPol{
  Param([String]$SNAPPOL,[String]$VOLNAME,[String]$SVMNAME)
  $a = Get-NcVol -Template
  $q = Get-NcVol -Template
  Initialize-NcObjectProperty $a VolumeSnapshotAttributes
  Initialize-NcObjectProperty $q VolumeIdAttributes
  $a.VolumeSnapshotAttributes.SnapshotPolicy = $SNAPPOL
  $q.VolumeIdAttributes.Name = $VOLNAME
  $q.VolumeIdAttributes.OwningVserverName = $SVMNAME
  [Void](Update-NcVol -Query $q -Attributes $a)
  IF((Get-NcVol -Name $VOLNAME -Attributes $a).VolumeSnapshotAttributes.SnapshotPolicy -eq $SNAPPOL){ RETURN $TRUE }
  ELSE{ RETURN $FALSE }   
}
FUNCTION Check-ApplyCmSnapPol {
  Param([String]$SNAPPOL,[String]$VOLNAME,[String]$SVMNAME,[String]$LOGFILE)
  [Boolean]$Check = Apply-CmSnapPol $SNAPPOL $VOLNAME $SVMNAME
  IF($Check){ Wr ("Applied snapshot policy $SNAPPOL to volume $VOLNAME") GREEN; Wr }
  ELSE{ Wr ("FAILED to apply snapshot policy $SNAPPOL to volume $VOLNAME") RED -Log $LOGFILE; Wr }
}

FUNCTION Wr {
  Param([String]$ToDisplay,[String]$Color = "WHITE",[String]$LogFile)
  If($ToDisplay){ Write-Host $ToDisplay -ForegroundColor $Color -NoNewLine } else { Write-Host }
  If($LogFile){ $ToDisplay >> $LogFile }
}

$SnapPol = Read-Host "SnapPol"
$Volname = Read-Host "Volume "
$Svmname = Read-Host "SVM    "

[Void](Check-ApplyCmSnapPol $SNAPPOL $VOLNAME $SVMNAME "Errors.txt")

No comments:

Post a Comment