Home > Software design >  Powershell with conditional stylesheet
Powershell with conditional stylesheet

Time:04-07

I need to create a report with the Headers color coded as in the screenshot. I want to assign #00FFFF to $Array_Good and #FF00FF to $Array_Bad. I believe I can do a conditional style-sheet, but I am not able to get it to work. I have specified th.good and tf.bad in the stylesheet but I am unsure how to call them using CLASS.

$Sr_No = 1
$Incident = "INC12345"
$Date_Submit = (Get-Date).ToString("MM/dd/yyyy")
$Client_Alias = "E9yu027"
$Access = "Full"
$MbxEmail = "[email protected]"
$SnowIncident = "4657748"

# Set the style for the email
$CSS = @"
<Style>
    table { margin: auto; font-family: Brandon Grotesque; border-collapse: collapse; }
    table, th, td { border: 1px solid #E3E3E3; }
    th.good{background: #00FFFF; color: #000000; max-width: 400px; padding: 5px 10px;}
    th.bad{background: #FF00FF; color: #000000; max-width: 400px; padding: 5px 10px;}
    td { font-size: 12px; padding: 5px 20px; color: #000; }
    tr { background: #fff; }
<⁄style>
"@


$Array_Good = @()
$Line_Good = (@{N="Initialize"}).N |  Select @{N="Sr";E={$Sr_No -join ";"}},
                @{N="Incident";E={$Incident -join ";"}},
                @{N="Date Submitted";E={$Date_Submit -join ";"}},
                @{N="Client Alias";E={$Client_Alias -join ";"}},
                @{N="Access";E={$Access -join ";"}},
                @{N="Mbx Email";E={$MbxEmail -join ";"}},
                @{N="Snow Incident";E={$SnowIncident -join ";"}}

$Array_Good  = $Line_Good
$Array_GoodHtml = ($Array_Good | ConvertTo-Html -Fragment)
$Array_BadHtml = $Array_GoodHtml
$Body = @()
$Body  = $CSS
$Body  = '<font size="4" face="Brandon Grotesque" >Shared Mailbox Access Successfully Granted:</font>'
$Body  = "<br>"
$Body  = $Array_GoodHtml
$Body  = "<br><br>"
$Body  = '<font size="4" face="Brandon Grotesque" >Problem granting access to Shared Mbx. Helpdesk Submitter to resolve:</font>'
$Body  = "<br>"
$Body  = $Array_BadHtml
$Body  = "<br><br>"

$Body_Final = $Body -join "`n" #convert to multiline string

Send-MailMessage -To $ToAddress -From $FromAddress -Subject $mailSubject -body $Body_Final -SmtpServer relay.server.com -BodyAsHTML

enter image description here

CodePudding user response:

Obviously not the prettiest way to do it but you could use string .Replace() to replace the <th> tags with your good and bad class attributes.

$style = @'
<style>
    table { margin: auto; font-family: Brandon Grotesque; border-collapse: collapse; }
    table, th, td { border: 1px solid #E3E3E3; }
    th { max-width: 400px; padding: 5px 10px;}
    th.good { background: #00FFFF; color: #000000; }
    th.bad { background: #FF00FF; color: #000000; }
    td { font-size: 12px; padding: 5px 20px; color: #000; }
    tr { background: #fff; }
</style>
'@

$obj = [pscustomobject]@{
    Sr            = 1
    Incident      = "INC12345"
    DateSubmitted = (Get-Date).ToString("MM/dd/yyyy")
    ClientAlias   = "E9yu027"
    Access        = "Full"
    MbxEmail      = "[email protected]"
    SnowIncident  = "4657748"
}

$html = [string]::Format(@"
<!DOCTYPE html>
<html>
{0}
<h3>Shared Mailbox Access Successfully Granted:</h3>
<br>{1}<br><br>
<h3>Problem granting access to Shared Mbx. Helpdesk Submitter to resolve:</h3>
{2}
</html>
"@,
    $style,
    [string]($obj | ConvertTo-Html -Fragment).Replace('<th>', '<th class=good>'),
    [string]($obj | ConvertTo-Html -Fragment).Replace('<th>', '<th class=bad>')
)

Which produces the following HTML:

<!DOCTYPE html>
<html>
<style>
  table {
    margin: auto;
    font-family: Brandon Grotesque;
    border-collapse: collapse;
  }
  
  table,
  th,
  td {
    border: 1px solid #E3E3E3;
  }
  
  th {
    max-width: 400px;
    padding: 5px 10px;
  }
  
  th.good {
    background: #00FFFF;
    color: #000000;
  }
  
  th.bad {
    background: #FF00FF;
    color: #000000;
  }
  
  td {
    font-size: 12px;
    padding: 5px 20px;
    color: #000;
  }
  
  tr {
    background: #fff;
  }
</style>
<h3>Shared Mailbox Access Successfully Granted:</h3>
<br>
<table>
  <colgroup>
    <col/>
    <col/>
    <col/>
    <col/>
    <col/>
    <col/>
    <col/>
  </colgroup>
  <tr>
    <th class=good>Sr</th>
    <th class=good>Incident</th>
    <th class=good>DateSubmitted</th>
    <th class=good>ClientAlias</th>
    <th class=good>Access</th>
    <th class=good>MbxEmail</th>
    <th class=good>SnowIncident</th>
  </tr>
  <tr>
    <td>1</td>
    <td>INC12345</td>
    <td>04/05/2022</td>
    <td>E9yu027</td>
    <td>Full</td>
    <td>[email protected]</td>
    <td>4657748</td>
  </tr>
</table><br><br>
<h3>Problem granting access to Shared Mbx. Helpdesk Submitter to resolve:</h3>
<table>
  <colgroup>
    <col/>
    <col/>
    <col/>
    <col/>
    <col/>
    <col/>
    <col/>
  </colgroup>
  <tr>
    <th class=bad>Sr</th>
    <th class=bad>Incident</th>
    <th class=bad>DateSubmitted</th>
    <th class=bad>ClientAlias</th>
    <th class=bad>Access</th>
    <th class=bad>MbxEmail</th>
    <th class=bad>SnowIncident</th>
  </tr>
  <tr>
    <td>1</td>
    <td>INC12345</td>
    <td>04/05/2022</td>
    <td>E9yu027</td>
    <td>Full</td>
    <td>[email protected]</td>
    <td>4657748</td>
  </tr>
</table>
</html>

  • Related