From e71b8546f9cc4820f755855437cd38d9b7e4d830 Mon Sep 17 00:00:00 2001 From: rfost52 <85522106+rfost52@users.noreply.github.com> Date: Sat, 26 Jun 2021 22:09:56 -0400 Subject: [PATCH] Submitting System Report Generator to Community Scripts --- .../scripts/community_scripts.json | 10 ++ .../Win_Collect_System_Report_And_Email.ps1 | 104 ++++++++++++++---- 2 files changed, 92 insertions(+), 22 deletions(-) rename {scripts_wip => scripts}/Win_Collect_System_Report_And_Email.ps1 (55%) diff --git a/api/tacticalrmm/scripts/community_scripts.json b/api/tacticalrmm/scripts/community_scripts.json index 6f0210d6..ce2454a2 100644 --- a/api/tacticalrmm/scripts/community_scripts.json +++ b/api/tacticalrmm/scripts/community_scripts.json @@ -714,5 +714,15 @@ "shell": "powershell", "category": "TRMM (Win):Active Directory", "default_timeout": "300" + }, + { + "guid": "962d3cce-49a2-4f3e-a790-36f62a6799a0", + "filename": "Win_Collect_System_Report_And_Email.ps1", + "submittedBy": "https://github.com/rfost52", + "name": "Collect System Report and Email", + "description": "Generates a system report in HTML format, then emails it", + "shell": "powershell", + "category": "TRMM (Win):Other", + "default_timeout": "300" } ] \ No newline at end of file diff --git a/scripts_wip/Win_Collect_System_Report_And_Email.ps1 b/scripts/Win_Collect_System_Report_And_Email.ps1 similarity index 55% rename from scripts_wip/Win_Collect_System_Report_And_Email.ps1 rename to scripts/Win_Collect_System_Report_And_Email.ps1 index 71c4ff0b..85d7996a 100644 --- a/scripts_wip/Win_Collect_System_Report_And_Email.ps1 +++ b/scripts/Win_Collect_System_Report_And_Email.ps1 @@ -9,12 +9,21 @@ Results are sent as a HTML file to C:\Temp and e-mailed based on provided parameters .EXAMPLE - Example is forthcoming + Must provide ALL parameter arguments in the following manner (failing to do so will cause the script to exit out prior to creating and sending the report): + + -agentname + -file + -fromaddress + -toaddress + -smtpserver
+ -password + -port <587 is the standard port for sending mail over TLS> .NOTES Change Log V1.0 Initial release and parameterization V1.1 Check for C:\Temp path prior to generating report + V1.2 Parameter checks with exit codes added Reference Links: @@ -24,6 +33,7 @@ param( + $agentname, $fromaddress, $toaddress, $smtpserver, @@ -32,17 +42,56 @@ param( $file ) +#Parameter Checks with exit codes + +if ([string]::IsNullOrEmpty($agentname)){ + write-host "You must directly enter a hostname or use the TRMM Script Variable {{agent.hostname}} to pass the hostname from the dashboard." + exit 1 +} + +if ([string]::IsNullOrEmpty($file)){ + Write-host "You must provide a file name with a .HTM extension." + exit 1 +} + +if ([string]::IsNullOrEmpty($fromaddress)){ + Write-host "You must provide a sender's email address." + exit 1 +} + +if ([string]::IsNullOrEmpty($toaddress)){ + write-host "You must provide a recipient's email address." + exit 1 +} + +if ([string]::IsNullOrEmpty($smtpserver)){ + write-host "You must provide a SMTP server address." + exit 1 +} + +if ([string]::IsNullOrEmpty($password)){ + write-host "You must provide a password for the SMTP server" + exit 1 +} + +if ([string]::IsNullOrEmpty($port)){ + write-host "A valid port number is required to send the report." + exit 1 +} + +else{ + $path = "C:\Temp" $destination = "$path\$file" if(!(Test-Path -Path $path)){ -write-host "Path does not exist. Creating path prior to generating report" +write-host "Path does not exist. Creating path prior to generating report." New-Item -Path "C:\" -Name "Temp" -ItemType "directory" } else{ - Write-host "Path alreaedy exists. Generating Report" + Write-host "Path alreaedy exists. Attempting to generate report." } @@ -56,7 +105,7 @@ $a = $a + "" #Heading -"

System Report For Agent

" | Out-File $destination -Append +"

System Report For $agentname

" | Out-File $destination -Append #Network Information @@ -89,23 +138,34 @@ Get-PSDrive | Where {$_.Used -ne $null} | Select Name, @{n='Used';e={[float]($_. Get-Printer | Select Name, Type, PortName | ConvertTo-HTML -Head "

Printers

" -body $a | Out-file $destination -append -#Send Email -# $fromaddress = "" -# $toaddress = "" -$Subject = "System Report for Agent" -$body = Get-Content $destination -# $smtpserver = "" #for example, smtp.office365.com -# $Password = "" -# $port = #for example, 587 + + try { + #Send Email + + $Subject = "System Report for $agentname" + $body = Get-Content $destination -$message = new-object System.Net.Mail.MailMessage -$message.IsBodyHTML = $true -$message.From = $fromaddress -$message.To.Add($toaddress) -$message.Subject = $Subject -$message.body = $body -$smtp = new-object Net.Mail.SmtpClient($smtpserver, $port) -$smtp.EnableSsl = $true -$smtp.Credentials = New-Object System.Net.NetworkCredential($fromaddress, $Password) -$smtp.Send($message) \ No newline at end of file + $message = new-object System.Net.Mail.MailMessage + $message.IsBodyHTML = $true + $message.From = $fromaddress + $message.To.Add($toaddress) + $message.Subject = $Subject + $message.body = $body + $smtp = new-object Net.Mail.SmtpClient($smtpserver, $port) + $smtp.EnableSsl = $true + $smtp.Credentials = New-Object System.Net.NetworkCredential($fromaddress, $Password) + $smtp.Send($message) + + write-host "System Report successfully sent via email." + + exit 0 + } + + catch { + write-host "An error occurrd. Please check your parameters, SMTP server name, or credentials and try again." + exit 1 + } +} + +exit $LASTEXITCODE \ No newline at end of file