0

I have a script that copies Veeam backups to a NAS, at the end the script sends a status email.

When I run the script manually I receive the mail correctly but in tasks scheduler, the script does the copy but doesn't send the mail. I don't understand why.

Try {

#####Defining computer Uptime function#####
function Get-Uptime {
   $os = Get-WmiObject win32_operatingsystem
   $uptime = (Get-Date) - ($os.ConvertToDateTime($os.lastbootuptime))
   $Display = "Uptime of " + $Uptime.Days + " days, " + $Uptime.Hours + " hours, " + $Uptime.Minutes + " minutes" 
   Write-Output $Display
}
$up = Get-Uptime

#####Defining encrypted password variable#####
$KeyFile = "AES.key"
$getkey = get-content $KeyFile

$day = Get-Date

#####Defining send-mail function#####
function sendmail($subject, $data)
{
    $to = "[email protected]"
    $from = "[email protected]"
    $SMTPServer = "smtp.gmail.com"
    $SMTPPort = "587"
    $Password = Get-Content "cred.txt" | ConvertTo-SecureString -key $getkey
    $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer,$SMTPPort);
    $smtp.EnableSsl = $true
    $smtp.Credentials = New-Object System.Net.NetworkCredential($to, $Password);
    $smtp.Send($to, $from, $subject, $data);
}



    # Formating Date for day-month-year
    $date = Get-Date -Uformat "%d-%m-%Y"

    #Mounting \\freenas.domain.local\veeam-backup as drive letter :
    New-PSDrive -Name "H" -PSProvider FileSystem -Root "\\freenas.domain.local\veeam-backup\"

    #Creating new directory with current date
    New-Item -Name "backup_$date" -Path "H:\" -ItemType Directory

    #Copying all veeam backup files to NAS in current date directory
    Copy-Item -Path "D:\vm-backup\*.vbk" -Destination "H:\backup_$date"

    #Remove directories and their files older than 3 days
    #Get-ItemProperty -Path "P:\backup $date" | where-object {$_.LastWriteTime -lt ($date).AddDays(-3)} | Remove-Item -Force -Recurse
    $Now = Get-Date
    $Days = "3"
    $TargetFolder = "H:\"
    cd $TargetFolder
    $LastWrite = $Now.AddDays(-$Days)

    $Folders = get-childitem -path $TargetFolder | 
    Where {$_.psIsContainer -eq $true} | 
    Where {$_.LastWriteTime -le "$LastWrite"} 

        foreach ($Folder in $Folders){

        write-host "Deleting $Folder" -foregroundcolor "Red"
        Remove-Item $Folder -recurse -Confirm:$false
        }

    cd C:

    #Unmount drive P:
    Remove-PSDrive -Name "H"


    #Successful backup mail
    $subject = "Veeam backup copy successful"
    $data = "Copying VMs on freenas is successfull on $day with an $up"

}

Catch {

    #Unsuccessful backup mail
    $subject = "Veeam backup copy failed"
    $data = $_.Exception.Message

}

Finally {

    sendmail $subject $data

}

I have tried to move my functions outside the try and catch, inside it, some functions outside some inside. It always works in manual mode but not in the tasks scheduler. But remember the script does the copy but doesn't send the email.

task scheduler

5
  • 1
    Try adding a start-transcript at the beginning of the script and then run it in the scheduler. It should provide a log file where you may find the error Commented Jul 3, 2018 at 10:18
  • 1
    $KeyFile = "AES.key" - are you sure that task scheduler starts PowerShell in the folder where the key is stored? You can either use full path for $KeyFile or Start in option in task scheduler to specify the folder in which you run the script. Commented Jul 3, 2018 at 10:23
  • 1
    In the task Scheduler, are you running it like PowerShell.exe <Absolutepath of filename\scriptname.ps1> ? Commented Jul 3, 2018 at 10:25
  • 1
    Is there a special reason why you're using the .Net.Mail.SmtpClient instead of Send-MailMessage? If not, try using the standard PoSh cmdlet. Commented Jul 3, 2018 at 10:38
  • Hi all,Thanks for the answers. Robdy was right, I had to specified the path of cred.txt and AES.key Olaf: I just found that block of code and it permitted me to do has I wanted. I'll have a look at Send-MailMessage. Thank you again! Commented Jul 3, 2018 at 11:52

2 Answers 2

1

Your issue here is the relative path:

$KeyFile = "AES.key"

Powershell Console starts in C:\Users\USERNAME, so your key path will be C:\Users\USERNAME\AES.key

Task Scheduler will use the LOCAL SYSTEM account by default, this account however starts in %Windir%\System32, so your key path would be C:\Windows\System32\AES.key

To fix this either provide the full path to they key in the script:

$KeyFile = "C:\Users\USERNAME\AES.key"

Or set the Start in (Optional) field in your task to C:\Users\USERNAME

Sign up to request clarification or add additional context in comments.

1 Comment

It would be more accurate to say that PowerShell starts in the running users home directory. That would cover system and regular users behaviour.In my experience setting the start in is not reliable and using absolute paths is the only guaranteed practice.
0

Thanks for the answers. Robdy was right, I had to specified the path of cred.txt and AES.key Olaf: I just found that block of code and it permitted me to do has I wanted. I'll have a look at Send-MailMessage. Thank you again!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.