Merge errors into HTML report

Welcome Forums General PowerShell Q&A Merge errors into HTML report

This topic contains 1 reply, has 2 voices, and was last updated by

 
Keymaster
11 months, 3 weeks ago.

  • Author
    Posts
  • #89902

    Participant
    Points: 0
    Rank: Member

    Hi guys,

    I'm struggling to merge the errors from $NotAvailableComputers into my HTML report (which is sent out via email).

    Powershell
    #Style Sheets
    $a = ""
    $a = $a + "h2, h5, th { text-align: center; font-family: Segoe UI; }"
    $a = $a + "table { margin: auto; font-family: Segoe UI; box-shadow: 10px 10px 5px #888; border: thin ridge grey; }"
    $a = $a + "th { background: #0046c3; color: #fff; max-width: 400px; padding: 5px 10px; }"
    $a = $a + "td { font-size: 11px; padding: 5px 20px; color: #000; }"
    $a = $a + "tr { background: #b8d1f3; }"
    $a = $a + "tr:nth-child(even) { background: #dae5f4; }"
    $a = $a + "tr:nth-child(odd) { background: #b8d1f3; }"
    $a = $a + ""

    $b = ""
    $b = $b + "h2, h5, th { text-align: center; font-family: Segoe UI; }"
    $b = $b + "table { margin: auto; font-family: Segoe UI; box-shadow: 10px 10px 5px #888; border: thin ridge grey; }"
    $b = $b + "th { background: #0046c3; color: #fff; max-width: 400px; padding: 5px 10px; }"
    $b = $b + "td { font-size: 11px; padding: 5px 20px; color: #000; }"
    $b = $b + "tr { background: #b8d1f3; }"
    $b = $b + "tr:nth-child(even) { background: #dae5f4; }"
    $b = $b + "tr:nth-child(odd) { background: #b8d1f3; }"
    $b = $b + ""

    #Script Variables
    $Computers = get-content "\\SERVER\list.txt"
    $OutFile = "\\SERVER\report\$(Get-Date -Format dd.MM.yyyy).html"
    #$errors = "\\SERVER\report\$(Get-Date -Format dd.MM.yyyy)_errors.html"

    #Test connection to servers
    Write-Host Testing connection to servers...

    $AvailableComputers = @()
    $NotAvailableComputers = @()
    foreach ($c in $Computers) {
    if (Test-Connection $c -Quiet -Count 1) {$AvailableComputers += $c} else { $NotAvailableComputers += $c }
    }

    #Print Error Report and store into Variable $ErrorReport
    Write-Host "Errors:"

    $ErrorReport = foreach ($Nac in $NotAvailableComputers) {
    Write-Host "Cannot connect to $nac" -BackgroundColor red #ConvertTo-Html -head $b -body "Errors:" | Out-File $OutFile -Append (DID NOT WORK)
    }

    #Get DiskSpace from AvailableComputers
    Write-Host Generating Disk Space Report..
    Get-WmiObject -Class Win32_LogicalDisk -ComputerName $AvailableComputers -Filter "DriveType = 3" |
    Select-Object -Property SystemName,DeviceID,@{Name="Total Size";Expression={$_.Size / 1gb -as [int] }},@{Name="Free Size";Expression={$_.freespace / 1gb -as [int] }} |
    ConvertTo-Html -head $b -body "WMC- Server Disk Space Report" |
    Out-File -FilePath $OutFile -Append

    # Email Disk space report to IT Services Team
    Write-host Sending email...

    $EmailSubject = "Disk Space report" + $DateTime
    $EmailTo = "example@domain.com"
    $body += Get-Content "\\SERVER\report\$(Get-Date -Format dd.MM.yyyy).html" | out-string
    #$body += Get-Content "\\SERVER\report\$(Get-Date -Format dd.MM.yyyy)_errors.html" | Out-String

    Send-MailMessage -To $EmailTo `
    -Subject $EmailSubject -From "example@domain.com" `
    -Body $Body -BodyasHTML `
    -SmtpServer "protection.outlook.com"

    My problem is when trying to present the errors in the HTML report – i can't figure out what I'm doing wrong, I get an empty table!

  • #90125

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    Consider using a Gist for long code listings – they turn out much easier to read ;). Just paste the Gist URL here and we pull it in from GitHub.

    Stop using Write-Host. Period. It doesn't do what you think; it draws pixels directly on the screen, and you can't rewrite those to a file. Ever.

    Look into the other Write commands. For example, your little "status report" messages should be using Write-Verbose. If you need to direct something to a file, consider using Add-Content – there's no reason to put it into a pipeline (Write-Output) if you just want it in a file. Also, you can't put colors into a file – another reason Write-Host doesn't redirect.

    Write-Host is one of the ultimate bugaboos in PowerShell and I've spent 11 years trying to squash it. Ah well :). Google "write host kills puppies" and you should get plenty of examples!

The topic ‘Merge errors into HTML report’ is closed to new replies.