<# .SYNOPSIS Generates a system report in HTML format. .DESCRIPTION A report comprised of stopped services, running processes, drive space, network adapter settings, and printers is stored locally with a copy sent via e-mail .INPUTS 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> .OUTPUTS Results are sent as a HTML file to C:\Temp and e-mailed based on provided parameters .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: www.google.com docs.microsoft.com #> param( $agentname, $fromaddress, $toaddress, $smtpserver, $password, $port, $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." New-Item -Path "C:\" -Name "Temp" -ItemType "directory" } else{ Write-host "Path alreaedy exists. Attempting to generate report." } #HTML Styling $a = "" #Heading "

System Report For $agentname

" | Out-File $destination -Append #Network Information Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'True'"| Select PSComputername, DNSHostName, Description, @{Name = "IPAddress";Expression = {[regex]$rx = "(\d{1,3}(\.?)){4}" $rx.matches($_.IPAddress).Value}},MACAddress | ConvertTo-HTML -Head "

Network Information

" -body $a | Out-file $destination -Append #Get Event logs Get-EventLog -LogName Application -Newest 10 -EntryType Error | Select TimeGenerated, EventID, Source, Message | ConvertTo-HTML -Head "

Application Error Event Logs

" -body $a | Out-file $file -Append Get-EventLog -LogName Application -Newest 10 -EntryType Warning | Select TimeGenerated, EventID, Source, Message | ConvertTo-HTML -Head "

Application Warning Event Logs

" -body $a | Out-file $file -Append Get-EventLog -LogName System -Newest 10 -EntryType Error | Select TimeGenerated, EventID, Source, Message | ConvertTo-HTML -Head "

System Error Event Logs

" -body $a | Out-file $file -Append Get-EventLog -LogName System -Newest 10 -EntryType Warning | Select TimeGenerated, EventID, Source, Message | ConvertTo-HTML -Head "

System Warning Event Logs

" -body $a | Out-file $file -Append #Get Stopped Services Get-Service | Where {($_.Status) -eq "Stopped"} | Select Status, Name, DisplayName | ConvertTo-HTML -Head "

Stopped Services

" -body $a | Out-File $destination -Append #Get Processes and CPU Get-Process | Select Id, ProcessName, CPU | ConvertTo-HTML -Head "

Processes & CPU

" -body $a | Out-File $destination -Append #Get Mapped Drives Get-PSDrive | Where {$_.Used -ne $null} | Select Name, @{n='Used';e={[float]($_.Used/1GB)}}, @{n='Free';e={[float]($_.Free/1GB)}}, Root| ConvertTo-HTML -Head "

Mapped Drives

" -body $a | Out-File $destination -Append #Get Printers Get-Printer | Select Name, Type, PortName | ConvertTo-HTML -Head "

Printers

" -body $a | Out-file $destination -append 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) 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