
param($action) # paramètre à scpécifier lors de l'appel

$Run_Path=Split-Path -parent $MyInvocation.MyCommand.Definition
$ErrorActionPreference = "Stop"
$resultlog="$Run_Path\Checkbackup.log"

<# Log
------------------------------------------------
Usage : Utilisé pour logger
$data : Informations à logger
$new : A préciser pour démarrer un nouveau log.
#>
Function Log ($data,$new) {
    If ($new) {
        write-output "$data" | Out-File $resultlog
    }
    Else {
        write-output "$data" | Out-File $resultlog -Append
    }
}

Log "################# START ###############" $true
Try {
    add-pssnapin Microsoft.Exchange.Management.PowerShell.SnapIn
}
Catch {

}

$global:ErrorCopy=@();$global:ErrorFull=@();$global:ErrorIncr=@()

$listdb=Get-MailboxDatabase -Status | select name, Server, LastFullBackup, LastIncrementalBackup, LastCopyBackup
Foreach ($db in $listdb) {
#Log $db
    If(! $db.Name.Contains("Mailbox")) {
        Switch ($action) {
            "COPY" {
                If (! $db.LastCopyBackup) {
                    $global:ErrorIncr+=$db.Name
                }
                Else {
                    If (($(Get-Date) - $db.LastCopyBackup).TotalMinutes -gt 35) {
                        $global:ErrorCopy+=$db.Name
                    }
                }
            }
            "FULL" {
                If (! $db.LastFullBackup) {
                    $global:ErrorIncr+=$db.Name
                }
                Else {
                    If (($(Get-Date) - $db.LastFullBackup).TotalDays -gt 7) {
                        $global:ErrorFull+=$db.Name
                    }
                }
            }
            "INC" {
                If (! $db.LastIncrementalBackup) {
                    If (! $db.LastFullBackup) {
                        $global:ErrorIncr+=$db.Name
                    }
                    Elseif(($(Get-Date) - $db.LastFullBackup).TotalDays -gt 1) {
                        $global:ErrorIncr+=$db.Name
                    }
                }
                Elseif(($(Get-Date) - $db.LastIncrementalBackup).TotalDays -gt 1) {
                    If (! $db.LastFullBackup)
                    {
                        $global:ErrorIncr+=$db.Name
                    }
                    Elseif(($(Get-Date) - $db.LastFullBackup).TotalDays -gt 1) {
                        $global:ErrorIncr+=$db.Name
                    }
                }
            }
        }
    }
}

If ($ErrorCopy.count -gt 0) {
    $string="Error on "
    $ErrorCopy=$ErrorCopy | sort
    Foreach ($dbname in $ErrorCopy) {
        $string+=$dbname+","
    }
    $string+=" during CopyBackup(Appsync)"
    Log $string;$string;exit 2
}

If ($ErrorFull.count -gt 0) {
    $string="Error on "
    $ErrorFull=$ErrorFull | sort
    Foreach ($dbname in $ErrorFull) {
        $string+=$dbname+","
    }
    $string+=" during FullBackup(TSM)"
    Log $string;$string;exit 2
}

If ($ErrorIncr.count -gt 0) {
    $string="Error on "
    $ErrorIncr=$ErrorIncr | sort
    Foreach ($dbname in $ErrorIncr) {
        $string+=$dbname+","
    }
    $string+=" during IncBackup(TSM)"
    Log $string;$string;exit 2
}
Log "################## END ####################"