Dima P. wrote:LinusH,
I am glad to hear that! Don’t forget to share the script examples with the community – it’s always highly appreciated.
I actually wrote a script so I could get emails and see log files but it turned out that my Veeam B&R installation mailed me anyway so I actually don't use it. But in case someone else is interested, perhaps if you don't have Veeam B&R, here is the powershell script. It uses a logging script i have borrowed from Luca on this site:
http://9to5it.com/powershell-logging-function-library/
Oh, my script have problems understanding if it had sent an email or not but since I don't use this script anymore, I haven't fixed it.
The script need this to function:
* The above mentioned logging script in the same folder.
* A subfolder called log
* Mail information
* In case you have installed Endpoint in a non-standard folder, you need to update that as well
* The exec policy must be set to Unrestricted for the scope LocalMachine like this:
Code: Select all
Set-ExecutionPolicy Unrestricted -Scope LocalMachine
Good luck!
Code: Select all
# Run_endpoint_backup.ps1
# Check if I am in the correct folder!
if (Test-Path ".\Run_endpoint_backup.ps1" -eq $False){
THROW "Wrong start folder, I need to be where the scrits are!"
}
#Script Version and author
$sScriptVersion = "1.0"
$sScriptAuthor = "Linus Hellsing"
#Date
$today = Get-Date
#$today = $today.ToString("dddd (yyyy-MM-dd)")
$time = $today.ToString("yyyy-MM-dd-HH-mm-ss")
$today = $today.ToString("yyyy-MM-dd_dddd")
# Logging
. ".\Logging_Functions.ps1"
# Log File Info
$sLogPath = ".\log"
$sLogName = "Endpoint_backup_" + $time + ".log"
$sLogFile = $sLogPath + "\" + $sLogName
# Email and servers
$email = "my.email@mydomain.com"
$Emailfrom = "Senderaddress <veeamserver@mydomain.com>"
$Server = $env:computername
$Smtp = "mysmtp.server.com"
# File with execution dates
$History_file = ".\exec_history.txt"
Log-Start -LogPath $sLogPath -LogName $sLogName -ScriptVersion $sScriptVersion
###################################
# Start your engines!
###################################
# Get the history if existing, else create. Count todays good backups (max 30).
$StatusSuccess = 0
if (Test-Path $History_file -eq $True){
Log-Write -LogPath $sLogFile -LineValue "Found history file $History_file"
$History = (Get-Content $History_file)[-1 .. -30] | Foreach-Object {
if ($_ -eq "$today 0") {$StatusSuccess = $StatusSuccess + 1}
}
}
Else{
New-Item $History_file -type file
Add-Content $History_file "This file is for keeping track of when backups have been created and their status. It was created: $today"
}
Log-Write -LogPath $sLogFile -LineValue "Successful backups today: $StatusSuccess"
# Set a default result code
$LastExitCode = "999"
Log-Write -LogPath $sLogFile -LineValue "Starts backup of $Server as $env:username"
$Param = "/backup"
. "C:\Program Files\Veeam\Endpoint Backup\Veeam.EndPoint.Manager.exe" $Param
$DidItWork = $?
$Return_code = $LastExitCode
Log-Write -LogPath $sLogFile -LineValue "Result: $Return_code Did it work: $DidItWork"
# In case the Lastexitcode is unchanged, lets use diditwork
if ($Return_code -eq "999" ) {
Log-Write -LogPath $sLogFile -LineValue "Result: $Return_code is unchanged. Uses this result instead: $DidItWork"
if ($DidItWork -eq "True" ) {
$Status = "a Success"
$Detailed_status = "Backup successfully created"
$Return_code = "0"
}
Else {
$Status = "a failure of some kind"
$Detailed_status = "Backup job failed to start or completed with error or was running"
}
}
Elseif ($Return_code -eq "0" ) {
$Status = "a Success"
$Detailed_status = "Backup successfully created"
}
Elseif ($Return_code -eq "-1" ) {
$Status = "a failure"
$Detailed_status = "Backup job failed to start or completed with error"
}
Elseif ($Return_code -eq "5" ) {
$Status = "actually still in progress"
$Detailed_status = "Backup job is currently running and cannot be started from the command line interface"
}
Else {
$Status = "... strange?"
$Detailed_status = "I got a return code I didn't know about: $Return_code. Check this link for more info: http://helpcenter.veeam.com/endpoint/11/backup_cmd.html"
}
Log-Write -LogPath $sLogFile -LineValue "Translation: the result was $Status"
Log-Write -LogPath $sLogFile -LineValue "$Detailed_status"
###################################
# Send HTML email
###################################
$SmtpClient = new-object system.net.mail.smtpClient
$mailmessage = New-Object system.net.mail.mailmessage
$SmtpClient.Host = $Smtp
$mailmessage.from = $Emailfrom
$mailmessage.To.add($email)
$mailmessage.Subject = "Backup of files on $Server was $Status"
$mailmessage.IsBodyHtml = $true
$mailmessage.Body = @"
<h3><font face=Calibri>Hi, </font></h3>
<h3><font face=Calibri>The file backup on <font color=red><strong>$Server</strong></font> has finished and it was <strong>$Status</strong></h3>
<h4>What does it mean?</h4>
Well, I don't really know, I'm just a script. <br />
Really.<br />
But I have learned a few things. And by using that enormous knowledge, I come to this conclusion:<br />
<br />
$Detailed_status <br />
<br />
All that only since I got this return code from the backup software: $Return_code<br />
BTW, I will only send one mail each day, unless I encounter problems.<br />
<br />
You might find more info about the return code here: <a href="http://helpcenter.veeam.com/endpoint/11/backup_cmd.html">Veeam help site</a>
Created: $today<br />
Executed as: $env:username
</font>
"@
# Send the mail only once per successful backup of the day or if it have failed
if ($StatusSuccess -eq "0") {
$smtpclient.Send($mailmessage)
Log-Write -LogPath $sLogFile -LineValue "Sends an email to $email. This is the first one or we have a problem."
}
elseif ($Return_code -ne "0") {
$smtpclient.Send($mailmessage)
Log-Write -LogPath $sLogFile -LineValue "Sends an email to $email. Problems!"
}
# Add this status on a new line
Add-Content $History_file "`n$today $Return_code"
###################################
# Delete old log files
###################################
$limit = (Get-Date).AddDays(-60)
$path = $sLogPath
Log-Write -LogPath $sLogFile -LineValue "Delete files older than $limit"
# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
## Delete any empty directories left behind after deleting the old files.
#Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse
Log-Finish -LogPath $sLogFile
# END
Edit: Forgot about the "ExecutionPolicy".