forked from BornToBeRoot/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScheduledTask_Logging.ps1
42 lines (31 loc) · 1.4 KB
/
ScheduledTask_Logging.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
###############################################################################################################
# Language : PowerShell 4.0
# Filename : ScheduledTask_Logging.ps1
# Autor : BornToBeRoot (https://github.com/BornToBeRoot)
# Description : Template to run a script as scheduled task with logging
# Repository : https://github.com/BornToBeRoot/PowerShell
###############################################################################################################
# Path to the logfile (maybe add date)
$LogPath = "$PSScriptRoot\Log.txt"
# Enable send mail (on error)
[bool]$SendMailOnError = $true
# Mail properties
$SmtpServer = ""
$MailFrom = ""
[String[]]$MailTo = @("","")
$MailSubject = "[Script Error] Title..."
# Clear the error variable
$Error.Clear()
# Specify or import credentials (if required)
$Cred = Invoke-Expression -Command "$PSScriptRoot\Get-ManagedCredential.ps1 -FilePath $PSScriptRoot\cred.xml"
# Start logging
Start-Transcript -Path $LogPath -Append
# Execute the script (for more details use -Verbose)
Invoke-Expression -Command "$PSScriptRoot\MYSCRIPT.ps1 -Parameter1 ""Test1"" -Parameter2 ""Test2"" -Credential `$Cred -Verbose"
if($SendMailOnError -and $Error.Count -gt 0)
{
# Send all "$Error"
Send-MailMessage -Subject $MailSubject -Body "$($Error | Out-String)" -SmtpServer $SmtpServer -From $MailFrom -To $MailTo
}
# End logging
Stop-Transcript