【问题标题】:Powershell script works in manual but not in task schedulerPowershell 脚本可以手动工作,但不能在任务调度程序中工作
【发布时间】:2023-03-09 15:58:01
【问题描述】:

我有一个将 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 和 catch 之外,在它内部,一些函数在一些内部之外。它始终在手动模式下工作,但不在任务调度程序中。 但请记住,脚本会复制但不会发送电子邮件。

【问题讨论】:

  • 尝试在脚本开头添加start-transcript,然后在调度程序中运行。它应该提供一个日志文件,您可以在其中找到错误
  • $KeyFile = "AES.key" - 您确定任务计划程序在存储密钥的文件夹中启动 PowerShell 吗?您可以在任务计划程序中使用 $KeyFileStart in 选项的完整路径来指定运行脚本的文件夹。
  • 在任务调度器中,你是不是像PowerShell.exe <Absolutepath of filename\scriptname.ps1>一样运行它?
  • 您使用.Net.Mail.SmtpClient 而不是Send-MailMessage 是否有特殊原因?如果没有,请尝试使用标准 PoSh cmdlet。
  • 大家好,感谢您的回答。 Robdy 是对的,我必须指定 cred.txt 和 AES.key Olaf 的路径:我刚刚找到了那个代码块,它允许我做我想做的事。我会看看 Send-MailMessage。再次感谢您!

标签: powershell


【解决方案1】:

您的问题是相对路径:

$KeyFile = "AES.key"

Powershell 控制台从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

【讨论】:

  • 更准确的说法是 PowerShell 从运行的用户主目录开始。这将涵盖系统和常规用户的行为。根据我的经验,设置起点并不可靠,使用绝对路径是唯一有保证的做法。
【解决方案2】:

感谢您的回答。 Robdy 是对的,我必须指定 cred.txt 和 AES.key Olaf 的路径:我刚刚找到了那个代码块,它允许我做我想做的事。我会看看 Send-MailMessage。再次感谢你!

【讨论】:

    猜你喜欢
    • 2020-12-05
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    相关资源
    最近更新 更多