Powershell脚本可以手动运行,但不能在任务计划程序中运行

时间:2018-07-03 10:02:20

标签: powershell

我有一个将Veeam备份复制到NAS的脚本,最后该脚本发送状态电子邮件。

当我手动运行脚本时,我可以正确接收邮件,但是在任务计划程序中,脚本会执行复制但不发送邮件。我不明白为什么。

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 = "mperry.it@gmail.com"
    $from = "mperry.it@gmail.com"
    $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

}

我试图将我的函数移到try外部,并在其中捕获某些函数,而在内部内部进行捕获。它始终在手动模式下工作,但在任务计划程序中不起作用。 但是请记住,脚本会执行复制操作,但不会发送电子邮件。

task scheduler

2 个答案:

答案 0 :(得分:1)

这里的问题是相对路径:

$KeyFile = "AES.key"

Powershell Console从C:\Users\USERNAME开始,因此您的密钥路径将为C:\Users\USERNAME\AES.key

默认情况下,任务计划程序将使用LOCAL SYSTEM帐户,但是此帐户以%Windir%\System32开头,因此您的密钥路径为C:\Windows\System32\AES.key

要解决此问题,请提供它们在脚本中键入的完整路径:

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

或将任务中的Start in (Optional)字段设置为C:\Users\USERNAME

答案 1 :(得分:0)

感谢您的回答。 Robdy是正确的,我必须指定cred.txt和AES.key Olaf的路径:我只是发现那段代码,它允许我做我想做的事情。我来看看Send-MailMessage。再次感谢你!