Michael
Michael

Reputation: 15

Powershell script works in manual but not in task scheduler

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

Upvotes: 0

Views: 1162

Answers (2)

Michael
Michael

Reputation: 15

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!

Upvotes: 0

henrycarteruk
henrycarteruk

Reputation: 13207

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

Upvotes: 1

Related Questions