Gatewarden
Gatewarden

Reputation: 393

Powershell triming and converting to HTML

I have a script using a NPRINT Api, I drag out name and lastexecution from the API. In the picture you can see how lastexecution looks. I want to trim T and foward, meaning I don't want to see anything after the date.

I tried working with | % {$_.name + " " + $_.lastexecution.Split('T')[0]} or similar, but it won't work due to the converting to HTML.

What should I use here?

Lastexecution

The code for the whole script

$Today = Get-Date -Format yyyyMMdd
$smtpServer = "secret"
#$mailSender = "secret"
$mailReceiver = "secret"
$mailSubject = "Nprint statusrapport $today"
$bodymånad = Get-NPTasks | Select-Object name,lastexecution | Where-Object {$_.name -match "tskPrimärvårdDetaljMånad*" } | ConvertTo-Html -head $head -PreContent $Head_Precontent1| out-string #| ConvertTo-Html -as Table | Out-String
$bodykomplett = Get-NPTasks | Select-Object name,lastexecution | Where-Object {$_.name -match "tskPrimärvårdDetaljKomplett*" } | ConvertTo-Html -head $head -PreContent $Head_Precontent2 | out-string #| ConvertTo-html -as Table | Out-String 
$Rad1 = "-Om mailet är tomt innebär det att NPrint ligger nere"
$Rad2 = "-Om alla månadsrapporter inte triggats behöver man logga på NPrint och kolla i publish task efter fel "
$Rad3 = "-Om alla komplettrapporter inte gått den 3:e behöver man logga på NPrint och kolla i publish task efter fel."

$Head_Precontent1 = "<H2>Månadsrapporterna skall gå varje dag</H2>"
$Head_Precontent2 = "<H2>Komplettrapporten skall gå den 3:e varje månad</H2>"
$bodycontent = "<H3>
$rad1<br>
$rad2<br> 
$rad3<br>
</H3>"

$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>
"@

$bodyend = @"


-Om mailet är tomt innebär det att NPrint ligger nere
-Om alla månadsrapporter inte triggats behöver man logga på NPrint och kolla i publish task efter fel 
-Om alla komplettrapporter inte gått den 3:e behöver man logga på NPrint och kolla i publish task efter fel.


"@

$output = $bodymånad + bodykomplett  + $bodycontent
Send-MailMessage -to $mailReceiver -subject $mailSubject -body $output -SmtpServer $smtpServer -BodyAsHtml

Upvotes: 0

Views: 38

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174900

Change your Select-Object statements from:

... | Select-Object name,lastexecution | ...

to:

... | Select-Object Name,@{Name='LastExecution';Expression={$_.LastExecution -split 'T' |Select-Object -First 1}} | ...

This will make PowerShell "re-calculate" the value of the LastExecution property before passing it to the downstream commands

Upvotes: 1

Related Questions