I am working with a script that uses the above line to create an html file containing data about failed logins, then attach the html file to an email.
How can I make the $eventsDC data become text placed into the body of an HTML email??
I have the code for sending the email, using PS 1.0 net.mail.mailmessage for now, and it all works to send the above $Report file as an attachment, but I want to try to have the $Report text converted into something that can become an email body.
I did try using a new variable and using Out-String but this wrote the output to the view/immediate window, not into anything that could be emailed, so far as I could see…
Yes!!! You’re on the right track with the second post. You pumped out everything to a file and then tried to pass that ‘file object’ to FuncMail and it got confused. It only sees that you passed it a file. FuncMail doesn’t know what it is or whats inside it. You say you want FuncMail to read the the files contents, interpret them as HTML code and insert them into an email???..but you don’t tell FuncMail that.
Lesson 1: Object types (Use the cmdlet Get-Member to analyze objects in more detail)
Apart from that, there were several other areas in your code that needed attention. When you call FuncMail you use a parameter -Body yet you do not use that param inside the function. You call $Report instead? FuncMail doesn’t know what $Report is because you haven’t defined it within the variable scope.
Lesson 2: Variable Scope
We could go on but it would be easier to just show you. This is how I might accomplish what you are doing.
I was indeed attempting to reuse other code wrt FuncMail.
So I will try your suggestion for emailing…I can still use the other code to generate the report file.
I think I can figure out how to append to $Body each time it does one of the 4 servers I am evaluating.
Thank you, Tom
It’s not THAT long a report, my earlier original code does the report and attaches to a message but I am now trying to get it into an email body.
It’s maybe 50-75 lines of HTML table lines, not a lot at all.
Thank you, Tom
Ohhhhhhhhhhh…now I understand what you are suggesting, I would like to do that…that line with $emailbody is how I concatenate the HTML outputs of $events###??
Thank you for feedback on curly braces though PoSh did not complain.
I wanted the body things to be concatenated so I have 4 different tables, one for each server.
I want the email sent after the four servers have been reviewed, that’s why send-mailmessage is outside the foreach loop, it’s wanted to send all four server results in one email.
Thank you, Tom
Oh, now I got ya. Then you’ll need to accumulate the $Events each time the loop iterates. So change $Events = to $Events +=. You will also need to define $Events as an array before the foreach loop b/c powershell complains if you attempt to add elements to an array when it doesn’t already know the variable is an array. So above the foreach add a line $Events = @().