繁体   English   中英

计划的Powershell任务未执行

[英]Scheduled Powershell task not being executed

我在任务计划程序上添加了powershell脚本任务,将用户帐户设置为Admin,然后将选项设置为“仅在用户登录时运行”。

当我手动运行此任务时,它会正确执行,但是当我将选项设置为“运行是否登录用户”时,它会执行,但永远不会成功完成任务。

在两种情况下都启用了“以最高特权运行”。 似乎正在发生什么? 我如何运行任务而无需登录?

编辑:

脚本将文件从已安装的驱动器复制到本地目录。 当我使用Powershell而不是任务计划程序逐行运行脚本时,它可以工作(在普通和提升的Powershell上)。

$currentDate = (Get-Date).AddDays(-1).ToString('yyyyMMdd');

gci "C:\some_directory" | where-object { ($_.Name -match $currentDate) -and (! $_.PSIsContainer) } | Copy-Item -Destination "Y:\" -force;

在任务计划程序上:Powershell-命令“ c:\\ scripts \\ my_script.ps1”

对于错误日志记录,需要将脚本分为更多可管理的块。 单线的会议很麻烦,但维护起来却不容易。

$currentDate = (Get-Date).AddDays(-1).ToString('yyyyMMdd')
$log = "c:\temp\logfile.txt"
$errorCount = $Error.Count

# Save the source files into an array
$files = @(gci "C:\some_directory" | ? {
  ($_.Name -match $currentDate) -and (! $_.PSIsContainer) 
})

# Log about source to see if $files is empty for some reason    
Add-Content $log $("Source file count: {0}" -f $files.Count)
# Check that Y: drive is available
Add-Content $log $("Testing access to destination: {0}" -f (test-path "y:\") )

$files | % {
  # Check the error count for new errors and log the message
  if($Error.Count -gt $errorCount) {
    Add-Content $log $Error
    $errorCount = $Error.Count
  }
  Copy-Item $_.FullName -Destination $(join-path "y:" $_.Name) -force
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM