此脚本检查数据库并标记是否有任何备份失败。它检查完整、差异和日志备份。
#Backup Check Version 1.0.
#List Server Name from
$ServerList = Get-Content "D:\PSScripts\BackupScript\ServerList.txt"
#Output file Location : Put is IIS if you want to publish
$OutputFile = "c:\IIS\BackupReport.htm"
$HTMLHDR=""
$BODYRED =""
$BODYGRN=""
$now=get-date
$FOOTER=""
$HTMLHDR += '<style type="text/css">
#Header{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width:700;border-collapse:collapse;}
#Header td, #Header th {font-size:12px;border:1px solid #98bf21;padding:3px 7px 2px 7px;}
#Header th {font-size:10px;text-align:center;padding-top:5px;padding-bottom:4px;background-color:#A7C942;color:#fff;}
#Header tr.alt td {color:#000;background-color:#EAF2D3;}
</Style>'
$HTMLHDR += "<HTML><HEAD><TITLE>Backup Page</TITLE></HEAD><BODY>
<p align=Center><font size=4 face=Bodoni MT color=#003333><B>Backup Report :Location</B></font></p>
<p align=Center><font face=Bodoni MT color=Green size=3>Last Pulled : $now IST</font></P>
<center><Table border=1 cellpadding=0 cellspacing=0 width=900 id=Header aling ='center'>
<TR>
<TH><B>Server Name</B></TH>
<TH><B>Database Name</B></TH>
<TH><B>RecoveryModel</B></TD>
<TH><B>Last Full Backup Date</B></TH>
<TH><B>Last Differential Backup Date</B></TH>
<TH><B>Last Log Backup Date</B></TH>
</TR>"
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
## Function if you need to skip or include any specific DB
function CheckDBName ($DBName){
$NameFlage ="true"
if($DBName -like "*DBName*") {$NameFlage ="false"}
return $NameFlage
}
##Get Backup details
ForEach ($ServerName in $ServerList)
{
if($ServerName -ne ""){
$DT = Get-WmiObject -Class Win32_LocalTime -ComputerName $servername
$DateTime = (Get-Date -Day $DT.Day -Month $DT.Month -Year $DT.Year -Minute $DT.Minute -Hour $DT.Hour -Second $DT.Second)
$SQLServer = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $ServerName
Foreach($Database in $SQLServer.Databases)
{
$FLAGE="false"
$color="Green"
$model=$($Database.RecoveryModel)
$DBName=$Database.Name
$FBKP=($DateTime-$Database.LastBackupDate).days
$DBKP=($DateTime-$Database.LastDifferentialBackupDate).days
$LBKP=($DateTime-$Database.LastLogBackupDate).hours
$WDay=$DateTime.DayOfWeek
if($(CheckDBName($Database.name)) -eq "true") {
$tmp= "<tr><TD>$($Servername)</TD><TD>$($Database.Name)</TD><TD>$model</TD>"
if($FBKP -ge 7){
$color="RED"
$FLAGE="true"
}
if($WDay -like "mon*"){
if($FBKP -ge 4){
$color="RED"
$FLAGE="true"
}}
$tmp+="<TD bgcolor='$color'>$($Database.LastBackupDate)</TD> "
$color="Green"
if($DBKP -ge 1 -AND $FBKP -gt 1){
$color="RED"
$FLAGE="true"
}
$tmp +="<TD bgcolor='$color'>$($Database.LastDifferentialBackupDate)</TD>"
$color="Green"
if($Model -like "*Full*"){
if($LBKP -ge 2){
$color="RED"
$FLAGE="true"
} }
$tmp +="<TD bgcolor='$color'>$($Database.LastLogBackupDate)</TD> </TR>"
$color="Green"
if($FLAGE -eq "TRUE"){$BODYRED +=$tmp}
else {$BODYGRN+=$tmp}
}
}
}
}
$FOOTER+= "</Table></BODY></HTML>"
$FILE=$HTMLHDR+$BODYRED+$BODYGRN+$FOOTER
$FILE | Out-File $OutputFile
Exit
###########END#########################