-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemailreport.ps1
76 lines (52 loc) · 2.42 KB
/
emailreport.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#written by: Mike
#Contact:
#
#Description: process for transfer weekly reports to people
#
#error handling and logging
#Set-StrictMode -Version latest
$Error.clear()
$Error
$ErrorActionPreference = "Stop"
#setting variables and loading configuration
$yesterday = get-date -date $(get-date).adddays(-1) -format yyyy-MM-dd
$thedaybefore = get-date -date $(get-date).adddays(-7) -format yyyy-MM-dd
$mailbox = 'princilple'
$email= 'test@blah.com'
$username = 'username'
$smtpserver = 'mail'
$domain = 'testing.local'
$intRec =0
try {
Write-Output "Gathering credentials and loading into memory"
#to create password run command with same users that userpowershell script runs as to create encrypted string.
#read-host -assecurestring | convertfrom-securestring | out-file C:\credentials\pwd.txt
#password for
$password2 = cat C:\credentials\pwd.txt | convertto-securestring
$Ptr2 = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($password2)
$Decryptpassword2 = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr2)
[System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr2)
#
$password=$Decryptpassword2|ConvertTo-SecureString -AsPlainText -Force
$UserCredential = New-Object System.Management.Automation.PSCredential $username, $password
Get-MessageTrackingLog -Server serverbox.local -Start $thedaybefore -End $yesterday -Recipients $email | ForEach {
# Received E-mails
If ($_.EventId -eq "DELIVER") {
$intRec += $_.RecipientCount
}
}
Write-Output "E-mail to $email from $thedaybefore to $yesterday was $intRec"
$body = "E-mail to $email from $thedaybefore to $yesterday was $intRec"
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
Send-MailMessage -From 'administrator@testing.com' -To 'user1@testing.com', 'user2@testing.com' -Bcc 'm@testing.com' -Subject 'Mail Report' -Body $body -SmtpServer $smtpserver
# -Credential $UserCredential -UseSsl
} catch {
# !! This shouldn't be necessary, but if we left $ErrorActionPreference
# !! 'Stop' in effect, the use of Write-Error itself would cause an
# !! - uncaught - terminating error.
$ErrorActionPreference = 'Continue'
# Write the error to the error stream or screen for testing
Write-Error $_
# Exit the script with a nonzero exit code to signal failure.
exit 1
}