Home > database >  Powershell html table won't show in mail with join
Powershell html table won't show in mail with join

Time:05-07

I started working on a script today to loop through the cert store on all servers to catch all certificates that are to expire within 30 days.

As soon as I started to loop the servers I added the -join to join all data from the loop and then the table will not show in the mail anymore, just the data without borders.

I don't get the table as designed even thou I used this code for the table a long time ago.

Also I would like to add $env:COMPUTERNAME above every table.

End goal is to have a table from each server if that is not clear but in the same mail.

Code:

$pathRead = "D:\temp\lista.txt"
$smtpserver = "-"
$mailsender ="-"
$mailReceiver = "-"
$mailSubject = "Certificates about to expire"
$dagarkvar=30

<#
$settings = @{
    "TargetServerURI" = "vmrydsqli01.rydsbilglas.internal";
    "TargetServerUser"      = "rydsglas\svc-qliksense-prod";
}
#>

$head=@"
<style>
@charset "UTF-8";

table
{
font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse:collapse;
}
td
{
font-size:1em;
border:1px solid #98bf21;
padding:5px 5px 5px 5px;
}
th
{
font-size:1.1em;
text-align:center;
padding-top:5px;
padding-bottom:5px;
padding-right:7px;
padding-left:7px;
background-color:#A7C942;
color:#ffffff;
}
name tr
{
color:#F00000;
background-color:#EAF2D3;
}
</style>
"@

$output = foreach($line in Get-Content $pathRead) { Invoke-Command -ComputerName $line -ScriptBlock { Get-ChildItem Cert:\LocalMachine\My | select Subject, FriendlyName, Thumbprint, Notafter | ConvertTo-Html -head $header | out-string 
 Where-Object {$_.NotAfter -gt (Get-Date) -and $_.NotAfter -lt (Get-Date).AddDays($Dagarkvar)} 
        }
       }
    


Send-MailMessage -From $mailsender -to $mailReceiver -subject $mailSubject -Body (-join $output ) -SmtpServer $smtpServer -Port 25 -BodyAsHtml

What am I doing wrong and how can I add $env:COMPUTERNAME as a header on each table to see which server the table is from.

Edit: With the answer I got with some extra rework I can extract the data but to a table but the problem is filtering only certificates expering within 30 days. Don't want so see stuff that epxires in 3 month or that has expired for example.

CodePudding user response:

As commented, you have the order of things in the scriptblock wrong by writing ConvertTo-Html ... before you do extra filtering on the object with a Where-Object clause. After the ConvertTo-Html, you're not dealing with objects anymore, but a formatted string instead.

Also, you have defined a variable $head, but use $header inside the scriptblock.

Simply have the loop emit objects you collect in variable $output and after the loop convert all that to Html.

Try:

$output = foreach($line in (Get-Content -Path $pathRead)) { 
    Invoke-Command -ComputerName $line -ScriptBlock { 
        Get-ChildItem Cert:\LocalMachine\My | 
        Where-Object {$_.NotAfter -gt (Get-Date) -and $_.NotAfter -lt (Get-Date).AddDays($Dagarkvar)} |
        Select-Object @{Name = 'ComputerName'; Expression = {$env:COMPUTERNAME}}, Subject, FriendlyName, Thumbprint, Notafter
    }
}

# create the html body. remove the extra properties added by PowerShell
$output = $output | Select-Object * -ExcludeProperty PS*, RunspaceId | 
          ConvertTo-Html -Head $head | Out-String
  • Related