powershell - 任务调度程序 powershell 脚本永远不会结束

标签 powershell task scheduler

我有一个手动运行很好的脚本。但是,当我在 Task Scheduler 中安排它时,脚本永远不会结束,因此下次尝试运行时,它会失败,因为前一个实例仍在运行。脚本本身在第一次或手动运行时需要几秒钟才能完成。这是脚本:

$source = "\\server1\upload"
$destination = "\\server2\upload"
$logfile = "c:\Scripts\fileMover\log.txt"
$table = Get-ChildItem $source -include *
foreach ($file in $table){
    $filename = $file.FullName
    #write-host $filename
    try
    {
        move-item -LiteralPath $filename -destination $destination -force
        $body = "Successfully moved $filename to $destination"
        $subject = "fileMover Succeeded"
    }
    catch
    {
        $body = "Failed to move $filename to $destination"
        $subject = "fileMover Failed"
    }
    finally
    {
        $body | out-file $logfile -append -width 1000 -encoding ascii
        Send-MailMessage -To "filemoverconfirmations@domain.com" -From "uploader@domain.com" -Subject $subject -SmtpServer "10.1.10.1" -Body $body
        exit
    }
}
exit

该脚本使用以下设置进行调度:
  • 无论用户是否登录都运行(用户帐户已被授予以批处理程序权限登录)
  • 以最高权限运行
  • 每天触发一次,每 2 分钟一次
  • 操作:启动程序 powershell -file c:\Scripts\upload.ps1

  • 作为一种解决方法,我将任务配置为在 1 分钟后自动停止。但是,我担心在某些情况下——例如大量大文件——脚本可能会在完全完成之前终止。

    该脚本需要每 2 分钟运行一次。

    最佳答案

    我在尝试执行 .ps1 时遇到了这个问题直接存档。执行 Powershell并将您的脚本作为参数提供给它。

    计划任务操作选项卡:

    Program\script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
    

    添加参数(可选):
    -command & 'E:\PowerShell_scripts\Script_name.ps1’

    关于powershell - 任务调度程序 powershell 脚本永远不会结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37142123/

    相关文章:

    cron - 验证来自 Google Cloud Scheduler 的 HTTP 请求

    powershell - $ _。SubString($ _。IndexOf (“”))Powershell帮助

    c# - 你如何使用 C# NetworkStream ReadAsync() 只抓取 stx 和 etx 之间的东西?

    c# - 带有输入参数和输出的 Task.Run

    multithreading - 调度器如何知道线程被阻塞等待输入?

    python - 如何在 Flask + Gunicorn 应用程序中使用后台调度程序

    regex - 将 ls 的结果通过管道传输到 Powershell 中的命令中,使用正则表达式捕获

    powershell - 如何在 Powershell 中过滤掉两次文本?

    sharepoint - 从 SPWeb 获取 SPWebTemplate

    gradle - Gradle 短任务