Add text before HTML table or in empty fields

by sabeltiger81 at 2013-02-11 06:38:43

This script scans users who have a logondate older then 90 days, and puts the users in an HTML table within the e-mail body. It’s all looking nice and for users that have never logged on, the days field are empty, which is okay.

But is it possible to add some info in the empty fields for users who have never logged on OR have a headlone before the "table of users", stating that If the "Days Column are empty users have never logged on". I have tried various combinations with and without variables, but I cannot make this happen.

I hope you guys can.

Import-Module ActiveDirectory
$HTMLStyle = "<style>"
$HTMLStyle = $HTMLStyle + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$HTMLStyle = $HTMLStyle + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:Beige; text-align: Center}"
$HTMLStyle = $HTMLStyle + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black; text-align: Center} </style>"

[String]$Body2 = Search-ADAccount -UsersOnly -SearchBase "OU=Users,OU=company,DC=subOU,DC=com" -AccountInactive -TimeSpan 90.00:00:00 | Where-Object{$.Enabled} | select Name,SamAccountName,@{n="Days";e={((Get-Date) - $.LastLogonDate).Days}} | sort Days -Desc | convertTO-HTML -HEAD $HTMLStyle

send-mailmessage -to user@EvilFirm.com -subject "subject" -from NiceAdmin@evilfirm.com -body $body2 -bodyasHTML -smtpserver Exchange.server.name
by coderaven at 2013-02-11 08:06:28
The way to pretty up your data in many cases like this is to update it before you go to HTML. The Expression in the hash table can contain logic.

[String]$Body2 = Search-ADAccount -UsersOnly -SearchBase "OU=Users,OU=company,DC=subOU,DC=com" -AccountInactive -TimeSpan 90.00:00:00 | Where-Object{$.Enabled} | select Name,SamAccountName,@{n="Days";e={ If ($.LastLogonDate -eq $null) {"Never"} else {((Get-Date) - $.LastLogonDate).Days}}} | sort Days -Desc | convertTO-HTML -HEAD $HTMLStyle
by sabeltiger81 at 2013-02-12 01:01:15
I thought that it might could use logic in the hash table, and I tried to make it work, well it did with your help. thanks for that. I have another question regarding this subject.

How do I get the fields "never" in the bottom of the table instead of top, and still have the highest numbers in the top if the table?

select Name,SamAccountName,@{n="Days";e={ If ($
.LastLogonDate -eq $null) {"Never"} else {((Get-Date) - $_.LastLogonDate).Days}}} |
by rasimmers at 2013-02-13 11:37:07
Use a number like a 0 or -1 to sort and then replace the text in the HTML.

$data = @()
$data += New-Object –TypeName PSObject -Property (@{‘Name’="Frank Thomas";‘SamAccountName’="fthomas1";‘Days’=33;})
$data += New-Object –TypeName PSObject -Property (@{‘Name’="Joe Smith";‘SamAccountName’="jsmith1";‘Days’="-1";})
$data += New-Object –TypeName PSObject -Property (@{‘Name’="Angie Johnson";‘SamAccountName’="ajohnso1";‘Days’=9;})

$html = $data | Sort-Object -Property Days -Descending | Select Name, SamAccountName, Days | ConvertTo-HTML -Fragment
$html -replace "-1", "Never"
by sabeltiger81 at 2013-02-14 03:08:48
@ rasimmers:

Do you suggest that I write the days a user has been inactive manually? Or do I misunderstand your suggestion?
by coderaven at 2013-02-14 06:19:25
What he is saying, is if you want to do some sorting of the days data, instead of using "Never" like I suggested, simply put in 0 or -1 so that the Sort command you used in your original example will work without anything special.
by sabeltiger81 at 2013-02-18 01:21:58
I thought that might be his point, so I have tested it now and it works perfect. I found that I also could put a dash "-" in, and then wite some tekst after that. Then it will also be put below the actual logon numbers.