I have multiple databases from multiple SQL Servers.
I am trying a output SQL data to an HTML table using Powershell from these databases/SQL Servers, then send a mail. The script I made works, but it will output multiple tables for each result in the html report. What I want is to append the data to the same table (Only 1 table containing all data). Struggling a bit on how to concatenate the data to only 1 table.
#Set flag to 0 for email notification (if any)
$alert= 0
$List = Get-Content -Path "C:\Users\testadmin\Documents\Log\serverlist.txt"
Foreach($Server in $ServerList){
$SQL = ($Server -split '=')[0]
$ = ($Server -split '=')[1]
$Query = "select host, Service, Status from Table with(nolock)"
$Value = Invoke-Sqlcmd -ServerInstance $SQL -Database $DB -Query $Query
foreach($mylocal in $Value ){
$hostname = $mylocal.host
$Service= $mylocal.Service
$Status = $mylocal.Status
if($Status -ne 'Running'){
$alert= 1
$Body += "<head>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
<style>
TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;}
TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
table.center {
margin-left: auto;
margin-right: auto;
}
</style>
<title>Warning</title>
</head>
<body>
<h3>Apps are Stopped !</h3>
<h4>Timestamp: $(Get-Date)</h4>
<table>
<tr>
<th>SQL</th>
<th>DB</th>
<th>Service Status</th>
</tr>
<tr>
<td>$SQL</td>
<td>$DB</td>
<td>$Status</td>
</tr>
</table>
</body>
"
}
else{
Write-Host ("no alert")
}
}
}
if($alert-eq '1'){
$Mail = @{
From = '[email protected]'
To = '[email protected]'
Subject = 'Warning!!'
smtpserver = 'myrelay.test.com'
Body = $Body
BodyAsHtml = $true
}
Send-MailMessage @Mail
}