Create a report and send it via email is easier than you think and it’s a really useful feature to automate recurring tasks. This particular example is inspired by another scripting library post about uptime. In fact, I will show you how to create a simple report with all servers bootup time and send it via email in few lines of code or even an oneliner if needed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#Requires -RunAsAdministrator #Paolo Frigo, https://www.scriptinglibrary.com # SETTINGS EMAIL REPORT $MailServer = "mail.server.com" $TargetServers = (Get-ADComputer -Filter * | Where-Object {$_.name -like "server-*" }).Name $ServerList = @{} #CHECK SERVER REBOOT TIME foreach ($Server in $TargetServers ) { Try { $BootUpTime = (Get-CimInstance -ClassName win32_operatingsystem -ComputerName "$Server" ).LastBootUpTime } Catch { $BootUpTime = "N/A" } $ServerList.Add($Server, $BootUpTime) } #GENERATE EMAIL REPORT Send-MailMessage -To "$MailRecipient" -From "$MailSender" -Subject "Reboot time of target servers" -Body "$($ServerList|Out-String)" -SmtpServer "$MailServer" exit 0 |