我有一个手动很好的脚本。但是,当我在任务计划程序中安排它时,脚本永远不会结束,因此下次尝试运行时,它会失败,因为先前的实例仍在运行。脚本本身需要几秒钟才能完成第一次或手动运行。这是脚本:
$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
该脚本使用以下设置进行安排:
作为解决方法,我将任务配置为在1分钟后自动停止。但是,我担心在某些情况下 - 例如大量的大文件 - 脚本可能会在完全完成之前被终止。
该脚本需要每2分钟运行一次。
尝试直接执行.ps1
文件时遇到此问题。执行Powershell
并将其作为参数提供给您。
计划任务操作选项卡:
Program\script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
添加参数(可选):
-command & 'E:\PowerShell_scripts\Script_name.ps1’