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

问题描述 投票:1回答:1

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

$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 "[email protected]" -From "[email protected]" -Subject $subject -SmtpServer "10.1.10.1" -Body $body
        exit
    }
}
exit

该脚本使用以下设置进行安排:

  • 运行是否记录用户(已授予用户帐户作为批处理程序权限登录)
  • 以最高权限运行
  • 每2分钟触发一次
  • 操作:启动程序powershell -file c:\ Scripts \ upload.ps1

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

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

powershell task scheduler
1个回答
0
投票

尝试直接执行.ps1文件时遇到此问题。执行Powershell并将其作为参数提供给您。

计划任务操作选项卡:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.