Hi Experts,
I need some formatting help in powershell using convert-html commandlet.
Basically, here is my requirement. I want to develop a powershell script which reads the SQL server errorlog and fetches Errors occurred in last 24 hours. That data has to be nicely formmated and sent as an email to the dba team.
Here is the script works fine but the formatting I am not getting properly. I have spent a lot of time but felt like if someone can help me on this.
Note: Output should contain 3 columns 1. Logdate, 2.ProcessInfo and 3.Text. Can anybody please help In the formatting piece or suggestions would be appreciated.
param(
[string]$inst=$null, # provide the instance name
#[datetime]$startdt=‘1900-01-01’,
#[datetime]$startdt=(get-date).AddDays(-1),
[datetime]$startdt=(Get-Date).AddHours(-24),
[string]$srch=$null
)
Load SMO assembly, and if we’re running SQL 2008 DLLs load the SMOExtended and SQLWMIManagement libraries
$v = [System.Reflection.Assembly]::LoadWithPartialName( ‘Microsoft.SqlServer.SMO’)
if ((($v.FullName.Split(‘,’))[1].Split(‘=’))[1].Split(‘.’)[0] -ne ‘9’) {
[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.SqlServer.SMOExtended’) | out-null
[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.SqlServer.SQLWMIManagement’) | out-null
}
Handle any errors that occur
Trap {
Handle the error
$err = $_.Exception
write-output $err.Message
while( $err.InnerException ) {
$err = $err.InnerException
write-output $err.Message
};
End the script.
break
}
clear-host
Connect to the specified SQL Server instance
$s = new-object (‘Microsoft.SqlServer.Management.Smo.Server’) $inst
Get the current error log
$err = $s.ReadErrorLog()
#$todaysdt = Get-Date
#Write-Host $todaysdt
#Write-Host “"
#Write-Host $startdt
#Write-Host "”
Initialize a new collection, then concatenate the errorlog properties together
$errlog = @()
$err | where {$.LogDate -ge $startdt} | foreach {
$errlog += [string] $.LogDate + ’ ’ + $.ProcessInfo + ’ ’ + $.Text
}
Search the errorlog and return any error and the subsequent detailed message
$results =@()
if ($srch -eq ‘DBCC’) {
$results = $results + $errlog | select-string -pattern ‘DBCC’ -context 0,0
}
else {
$results = $results + $errlog | select-string -pattern ‘Error:’ -context 0,1
}
Clear-Host
Write-Host $results
$results | Out-File C:\Scripts\Test.htm
Invoke-Expression C:\Scripts\Test.htm
Template for HTML
$html = @"
BODY{
background-color:white;
}
TABLE{
font-family: Arial;
font-size: 12px;
width:100%;
height:75%;
border-width: 1px;
border-style: solid;
border-color: black;
border-collapse: collapse;
}
TH{
border-width: 1px;
padding: 0px;
border-style: solid;
border-color: black;
color:white;
background-color: green;
}
TD{
border-width: 1px;
padding: 0px;
border-style: solid;
border-color: black;
background-color:white
}
"@
$FilePath = “c:\scripts” # this is the place where output html files gets created
$OutFile = Join-Path -path $FilePath -childPath (“SQLErrors_” + (get-date).toString(‘yyyyMMdd_hhmmtt’) + “.html”)
$results | ConvertTo-HTML -head $html -body " SQL Server ERRORS from ERRORLOG : "| Out-file $OutFile
Invoke-Expression $OutFile
Thanks,
-M