我有一个手动运行很好的脚本。但是,当我在 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
该脚本使用以下设置进行调度:
作为一种解决方法,我将任务配置为在 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/