MEM-Zone / MEM.Zone

Our ♡ collection of PowerShell scripts and Endpoint Management related stuff 🐵
https://MEM.Zone/GIT
GNU General Public License v3.0
97 stars 10 forks source link

[FEATURE] Would it be possible to add the ability to email a report, or send to a Teams Channel via webhook? #3

Closed RobSheppard1976 closed 2 years ago

RobSheppard1976 commented 3 years ago

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

It's not related to a problem. But thanks for potentially considering it!

Describe the solution you'd like A clear and concise description of what you want to happen.

A formatted email, containing the list of computers that were disabled, so as to inform managers/colleagues/Service Desk of the computers that were disabled, should a user call in.

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

I've cobbled together my own, but it could be more elegant.

Additional context Add any other context or screenshots about the feature request here.

RobSheppard1976 commented 3 years ago

Here's what I have for the email formatting.

Function Set-AlternatingRows {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory,ValueFromPipeline)]
        [string]$Line,

        [Parameter(Mandatory)]
        [string]$CSSEvenClass,

        [Parameter(Mandatory)]
        [string]$CSSOddClass
    )
    Begin {
        $ClassName = $CSSEvenClass
    }
    Process {
        If ($Line.Contains("<tr><td>"))
        {   $Line = $Line.Replace("<tr>","<tr class=""$ClassName"">")
            If ($ClassName -eq $CSSEvenClass)
            {   $ClassName = $CSSOddClass
            }
            Else
            {   $ClassName = $CSSEvenClass
            }
        }
        Return $Line
    }
}

function inactivecomputerreport {
$Style = "
<style>
  TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
  TH{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:#778899;text-align: left;}
  TD{border-width: 1px;padding: 3px;border-style: solid;border-color: black;text-align: left;}
  .odd { background-color:#ffffff; }
  .even { background-color:#dddddd; }
</style>
"
$pcdata = $computers | Select-Object -property Name, distinguishedName | Sort-Object -Property Name | ConvertTo-Html -Head $Style -as Table | Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd

$body = "
<h3>Delete Inactive Computers</h3>
$computercount inactive $us.
<br><br>
The following $us are disabled and have been inactive for $NumOfDaysInactiveFor days or more, and have therefore been deleted
<br><br>
$pcdata
<br><br>
Report generated $Today.
<br><br>
This report is running as a scheduled task on ComputerName
"
Ioan-Popovici commented 3 years ago

Sorry for the delay, I'm almost done. Ofc I had to overcomplicate things.

I had some problems with the HTML formatting because I wanted to make the code reusable. So now I have Format-HTMLReport cmdlet that can handle any table input with default templates that can be customized.

There's one catch, for the email to be sent securely I needed to use Mailkit and Mailmime, so these need to be installed via NuGet on the machine running the script. Unfortunately, .net and the current PowerShell versions do not support sending mail securely and the Send-MailMessage cmdlet was deprecated and is obsolete.

Another problem would be storing the password in plain text in the PowerShell script which I can't recommend. You have the option to do that but I would suggest using the parameters (however verbose they might be) instead.

In order to reduce the parameter creep the script has two variables that need to be passed as a hashtable. I don't really like this option, a config file would be preferable, I might refactor the code for this purpose. I'll probably ship the final version on Monday.

Do you still need the Teams Webhook?

By the way thanks for the alternating rows solution :D

Preview: CleanShot 2021-06-18 at 23 05 45@2x

Ioan-Popovici commented 3 years ago

Working on adding teams webhook support. It was a bit complicated posting a table in teams but I think I have a solution.

RobSheppard1976 commented 3 years ago

Wow, this looks great. I was going to say that a Teams webhook wasn't a necessity, was more curious. But it looks like you might have a solution. This is amazing! Regarding the email support, would either option 1 or 2 be useful for this?

https://docs.microsoft.com/en-us/exchange/mail-flow-best-practices/how-to-set-up-a-multifunction-device-or-application-to-send-email-using-microsoft-365-or-office-365

Ioan-Popovici commented 3 years ago

The email part is done, you just need to use powershell 7 and the mailkit and mimekit packages from nuget. I'll publish what I have on monday and then probably work on the teams integration afterwards 😉

Ioan-Popovici commented 3 years ago

Hi Rob,

Can you please test this beta version until I finish teams support? https://github.com/MEM-Zone/MEM.Zone/tree/master/PowerShell/Invoke-ADInactiveDeviceCleanup

You need to edit the configuration at the end of the script

Thanks

Ioan-Popovici commented 3 years ago

Ok, teams is very limited in what it can receive. Right now there is no support to attach files and if I send a list using columnset it really looks like crap. I don't think this is useful. You can still have a notification, but with no data, I don't know how useful that would be. I can maybe include a link in the notification but then I will need to upload that file in azure blob storage or something. Is this something you are interested in @RobSheppard1976 ?

RobSheppard1976 commented 3 years ago

@Ioan-Popovici, thanks for investing so much effort in it, I appreciate it very much. It doesn't sound like it's going to be viable/useful, which is unfortunate. I wouldn't invest any more time on my behalf at this point. Again, I really appreciate the work you've done so far. In terms of evaluating the script, I haven't had the chance. Last time I ran 'our' script, it worked too well, and disabled accounts for computers that hadn't logged into the VPN for a long time. Needless to say, that had a big ripple effect. So, until we form a process around that, I've been sworn off running something like that again. Unfortunate, but there it is. You've done a fantastic job though, I really do appreciate the work you've put in.

Ioan-Popovici commented 2 years ago

Finally got to this. It's not possible at the time to send the data to Teams Channel due to Teams API limitations.

I've moved the Parameters to a JSON file and put this out of beta.

https://MEM.Zone/Invoke-ADInactiveDeviceCleanup