Create a Backup Log for your Exchange 2007 Server (Powershell)

Creates a Backup log of the Active node of the Exchange 2007 Server.

Searches the Event Log for VSS Backup Events (213,225) and Calculates the Backup  time, Outputs the time the DB was lsat backed up, the DB size, Events Collected regarding the Exchange VSS Backup and the Total Backup time.


Also Checks if there was no backup in the last day and alerts on it.

Outputs all to a nice formated HTML.


Download the Script


 

 


################################################
# Created By Assaf Miron                       #
# Backup Log for your Exchange 2007 Server.ps1 #
################################################

# Import the Exchange 2007 Powershell cmdlets
Add-PSSnapin "Microsoft.Exchange.Management.Powershell.Admin"

# Get the Current Computer Name
$CompName = Hostname

# Get the Exchange 2007 Mailbox Server Name
$MBXServer = "MAilboxServerName"

# Define a Header for the HTML File
$Header = "<h1>Backup Status of Active Node - "+$CompName+"</h1>"

#Get the Time and day of the backup (because its after 12 then we deduct a day)
$Time = Get-Date
$Day = [datetime]::Now - [timespan]1d

#Define Exchange Backup Log Path
$ExLogFile = "C:\Scripts\ExchangeBackupLogs"

#Configure the Log File Name
$LogFile = $ExLogFile+"_"+$Day.DayOfWeek+".html"

#Define CSS for the Log File
$ImagePath = "http://ImageServerName/images"
$CSS = "<style type=`"text/css`">
TABLE{
    direction:LTR;
    border-collapse:collapse;
    border-style: solid;
    border-width: 1px;
    border-Color: Black;
    font-family: Calibri;
}
td{
    direction: LTR;
    border-style: solid;
    border-width: 1px;
    border-Color: Black;
    font-family: Calibri;
    background-repeat: no-repeat;
    background-position: center;
}
th{
    border-style: solid;
    border-width: 1px;
    border-color: Black;
}
body
{
    font-family: Calibri;
    background-image: url('$ImagePath/side1.jpg');
    background-repeat: no-repeat;
    background-position: top right;
}
</style>"


Function EDBSize([string]$DBName,[int]$DBType)
{
    $EDBFile = ""
    If ($DBType -eq 1)
    {
        $EDBFile = Get-MailboxDatabase -Identity $DBName | Select EDBFilePath
    }
    Else
    {
        $EDBFile = Get-PublicFolderDatabase -Identity $DBName | Select EDBFilePath
    }

    $objFile = new-object System.IO.FileInfo($EDBFile.EDBFilePath)
    $DBSize = ($objFile.get_Length()/1GB).ToString("##,##.### GB")
    Return $DBSize
}

Function isBackupEnd
{
#Get all the Related Events and write them in the log
$events = Get-EventLog -newest 2000 -LogName application | Where {(($_.EventID -eq "213") -and ($_.TimeWritten -gt $Day)) -or (($_.EventID -eq "225") -and ($_.TimeWritten -gt $Day))-or (($_.InstanceID -eq "3223922230") -and ($_.TimeWritten -gt $Day))} |foreach {"<BR>"+$_.TimeWritten+"<BR>"+$_.Message.Split(".")[0]+"<BR>" | Out-File -append $LogFile}

return ($events)

}

Function BackupTime($Table,$events)
{
#Calculate the Backup Time from the Logs and the Backup Table from the Exchange Server - Saved in the $Table Var

For ($i = 0; $i -lt $events.Count; $i++)
 {
    $EvntSGName = $Events[$i].Message.ToString().Split(".")[0] #.Substring($Events[$i].Message.IndexOf(")")+1,$Events[$i].Message.IndexOf("."))
    For($TblSG = 0; $TblSG -lt $Table.Count-1; $TblSG++)
    {
        $SGName = $Table[$TblSG].StorageGroup.ToString().Split("\")[1]
        If($EvntSGName.ToString() -match $SGName)
        {   
            $ETime = $Events[$i].TimeGenerated
            $SGTime = $Table[$TblSG].LastFullBackup
            $BackupTime = ($ETime - $SGTime)
            If($BackupTime -gt 1)
            {
                $Table[$TblSG].BackupTime = $BackupTime
            }
        }
    }
 }

}


Write-Host "Backup of the Active Node - $CompName - is Starting..." -Backgroundcolor Red -Foregroundcolor Yellow

# Get the Storage group Last Full Backup Time
    $MBXStatus = Get-MailboxDatabase -Server $MBXServer -Status | select Status,Name,StorageGroup,LastFullBackup,BackupTime,DBSize
    $PFStatus = Get-PublicFolderDatabase -Server $MBXServer -Status | select Status,Name,StorageGroup,LastFullBackup,BackupTime,DBSize
    $Table = $MBXStatus + $PFStatus
   
    $GetEvents = isBackupEnd
    BackupTime($Table,$GetEvents)

# Configure 1 Day Time Span   
    $1Day = [Timespan]1d
    $Date = [DateTime]($Time.Ticks - $1Day.Ticks)
    $Counter = 0

# Check if the last backup time is over one day and style it
    Switch -regEX ($Table)
    {
        "\d+/\d+/\d+"
        {
# Start Checking the dates
            $res = $_.LastFullBackup - $Date
   
# Asume that everything is OK ! all the backups succeeded
          $strSGName = $_.StorageGroup.ToString()
            $strDBName = $_.Name.ToString()
            $DBType = 1
            If ($PFStatus.Name -eq $strDBName)
            {
                $DBType = 2
            }
           
            $strEDB = $strSGName+"\"+$strDBName
            $_.DBSize = EDBSize $strEDB $DBType
            $_.Status = "<><Center><IMG SRC='$ImagePath/correct.jpg' WIDTH='24' HEIGHT='24'></center>"
            $_.Name = "<align=center>"+$_.Name
            $_.StorageGroup = "<align=center>"+$_.StorageGroup
            $_.LastFullBackup = "<align=center>"+$_.LastFullBackup
            $_.BackupTime = "<align=center>"+$_.BackupTime

# If Backup Time equals to 0 then Background color is yellow
             if($res.Days -eq 0) {
            if($res.Hours -lt 0) {
              Write-Host "One day"
              $_.Status = "<><Center><IMG SRC='$ImagePath/attention.jpg' WIDTH='24' HEIGHT='24'></center>"
              $_.Name = "<bgcolor='yellow'; align=center>"+$_.Name
              $_.StorageGroup = "<bgcolor='yellow'; align=center>"+$_.StorageGroup
              $_.LastFullBackup = "<bgcolor='yellow'; align=center>"+$_.LastFullBackup
              $_.BackupTime = "<bgcolor='yellow'; align=center>"+$_.BackupTime
              $Counter += 1
            }
            }
# If Backup Time is less than 0 then Background color is red
           if($res.Days -lt 0) {
          Write-Host "Greater than one day"
          $_.Status = "<><Center><IMG SRC='$ImagePath/attention.jpg' WIDTH='24' HEIGHT='24'></center>"
          $_.Name = "<bgcolor='red'; align=center>"+$_.Name
          $_.StorageGroup = "<bgcolor='red'; align=center>"+$_.StorageGroup
          $_.LastFullBackup = "<bgcolor='red'; align=center>"+$_.LastFullBackup
          $_.BackupTime = "<bgcolor='red'; align=center>"+$_.BackupTime
          $Counter += 1
            }
        }
    }
    If($counter -gt 0) {$StatusIMG = "<IMG SRC='$ImagePath/alert.jpg' WIDTH='48' HEIGHT='48' BORDER='0' ALT='Backup Not Complete'>" }
    Else {$StatusIMG = "<IMG SRC='$ImagePath/Correct.jpg' WIDTH='48' HEIGHT='48' BORDER='0' ALT='Backup Complete'>" }

# Write Data to Log File
    ConvertTo-Html -body $Header$StatusIMG -Head $CSS"<Title>"$CompName"</Title>" | Out-File $LogFile
    $Table | ConvertTo-HTML | Out-File -append $LogFile
    "<HR><BR/>" | Out-File -append $LogFile
   
    $GetEvents | Out-File -append $LogFile

# Insert Company Logo
    "<p><BR><Center><IMG SRC='$ImagePath/Logo.jpg' WIDTH='300' HEIGHT='180' BORDER='0' ALT='' align=center></Center></p>" | out-File -append $Logfile

# Do some HTML Parsing
    $tmp = Get-Content $LogFile
    $tmp = $tmp -replace("&gt;&lt;","><")
    $tmp = $tmp -replace(">&lt;"," ")
    $tmp = $tmp -replace("&gt;",">")

# Write all to Log
    $tmp | out-file $LogFile
    Write-Host "Backup of the Active Node - $CompName - Ended Now : " $Time.ToLongDateString() -Backgroundcolor Red -Foregroundcolor Yellow

Comments