Reputation: 69
I have been racking my brain for hours. All I want is the following.
202011.txt
3 12/28/2020 4:22:56.008 PM Front MOTION
10 12/28/2020 4:23:02.619 PM Joessomebody ::ffff:10.60.0.169: Login
3 12/28/2020 4:23:30.610 PM Front MOTION
3 12/28/2020 4:24:05.131 PM Front MOTION
It keeps looking like this in the email
3 12/28/2020 4:22:56.008 PM Front MOTION 10 12/28/2020 4:23:02.619 PM Joessomebody ::ffff:10.60.0.169: Login 3 12/28/2020 4:23:30.610 PM Front MOTION 3 12/28/2020 4:24:05.131 PM Front MOTION
$SmtpClient = new-object system.net.mail.smtpClient
$MailMessage = New-Object system.net.mail.mailmessage
$SmtpClient.Host = "campus.Test.com"
$mailmessage.from = ("[email protected]")
$mailmessage.To.add("[email protected]")
function Get-LastLinesNew
{
$v = Get-Content -Path "C:\BlueIris\log\202011.txt" -Tail 5
return ($v)
}
$mailmessage.Subject = “Alert - NVR - Blue Iris - Logs”
<#$mailmessage.Body = “Alert Body with Instruction to recipients”#>
$file = "C:\BlueIris\log\202011.txt"
$attach = Get-LastLinesNew
$attach = $attach -replace "'r'n","<br />"
$MailMessage.IsBodyHtml = $true
$attach #For Testing
$MailMessage.body = $attach -replace "'r'n","<br />"
$MailMessage.body #For Testing
pause #For Testing
try
{
$smtpclient.Send($mailmessage)
}
catch [System.Management.Automation.MethodInvocationException]
{
$MailMessage.Attachments.Dispose()
$MailMessage.Body = "File Size to large, ERROR"
$MailMessage.Subject = "NVR ERROR"
$smtpclient.Send($MailMessage)
}
I just stripped down the function to the bare minimum and its still fails
$MailMessage = New-Object system.net.mail.mailmessage
$attach = Get-Content -Path "\\NVR\c$\BlueIris\log\202011.txt" -Tail 5
$attach = $attach -replace "'r'n","<br />"
$MailMessage.IsBodyHtml = $True
$attach
$MailMessage.body = $attach
$MailMessage.body
pause
Also PSVersion 5.1.19041.610
Upvotes: 1
Views: 181
Reputation: 1111
A couple other options is to use [System.IO.File] or the -raw flag with Get-Content because Get-Content returns an array of lines.
function Get-LastLinesNew
{
$v = [System.IO.File]::ReadAllText("C:\BlueIris\log\202011.txt")
return ($v)
}
function Get-LastLinesNew
{
$v = Get-Content -Path "C:\BlueIris\log\202011.txt" -Raw
return ($v)
}
Upvotes: 0
Reputation: 69
In the end I needed to add Out-String as a piped command to the Get-Content
function Get-LastLinesNew
{
$v = Get-Content -Path "C:\BlueIris\log\202011.txt" -Tail 5 | Out-String
return ($v)
}
Just like that and it worked like a dream. I am guessing that the powershell shell has some sort of auto formatting built into it that allowed it to show all the strings as one string when using the replace command and then showing it on the screen. Once it went to the mail message it read it as an array again and lost all the formatting. Weird but solved. Thanks everyone!!!!
Upvotes: 1