计划的Powershell任务,卡住​​了跑步

ola*_*ell 5 windows powershell scheduled-tasks

在计划任务中运行PowerShell脚本时遇到问题.即使Transcript已经记录了最后一行"Done!",它仍然停留在Running中.而且看起来它完全符合我的要求.我错过了什么?

RunWindows中运行它似乎也很好:powershell -file "D:\_temp\copy_bat\copy_one_reprint_folder.ps1"

任务计划程序中的Seetings是:

  • 以完全的特权运行
  • 运行这两个用户是否已登录
  • 行动,启动计划 powershell.exe
    • -file "D:\_temp\copy_bat\copy_one_reprint_folder.ps1"

坚持跑步 TS的历史

如果需要,请参阅下面的代码.

# PowerShell RePRINT copy first folder, sorted on lastModified
function timestamp
{
    $ts = Get-Date -format s
    return $ts
}

$fromDirectory = "D:\_temp\copy_bat"
$toDirectory = "D:\_temp\in_dummy"
$extractGUIDdir = ""
$docTypeDir = ""

# Logging
#########
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path $fromDirectory\copy_one_reprint_folder.log.txt -append
###########


Write-Host ""
Write-Host (timestamp) "Copy RePRINT extract started"
Write-Host (timestamp) "============================"

Get-ChildItem -path $fromDirectory | ?{ $_.PSIsContainer } | Sort-Object CreationTime | `
    Where-Object {$_.name -ne "_copied"} | `
    Select-Object -first 1 | `
        Foreach-Object{
            Write-Host (timestamp) $_.name
            $extractGUIDdir = $_.FullName
            Get-ChildItem -path $extractGUIDdir |  ?{ $_.PSIsContainer } | Where-Object {$_.Name -match "Purchase Order \(-999999997\)" -or $_.Name -match "Logistics documents \(-1000000000\)" -or $_.Name -match "Specifications \(-999999998\)"} | `
                Foreach-Object{
                    Write-Host (timestamp) "  " $_.Name
                }
            Write-Host ""

            Write-Host "These folders (document types), were also found but will not be included"
            Get-ChildItem -path $extractGUIDdir -Exclude "Logistics documents (-1000000000)", "Purchase Order (-999999997)", "Specifications (-999999998)" | ?{ $_.PSIsContainer } | `
                Foreach-Object{
                    Write-Host (timestamp) " - " $_.name
                }
            Write-Host ""

            Get-ChildItem -path $extractGUIDdir | ?{ $_.PSIsContainer } | Where-Object {$_.Name -match "Purchase Order \(-999999997\)" -or $_.Name -match "Logistics documents \(-1000000000\)" -or $_.Name -match "Specifications \(-999999998\)"} | `
                Foreach-Object{
                        $temp_name = $_.FullName
                        Write-Host (timestamp) "copying files from " $_.FullName
                        Write-Host (timestamp) "                    to "  $toDirectory
                        #Copy-Item ($_.FullName)\*.* $toDirectory
                        Write-Host (timestamp) " copying meta-files..."
                        Copy-Item $temp_name\*.meta $toDirectory -Filter *.meta
                        Write-Host (timestamp) " copying pdf-files..."
                        Copy-Item $temp_name\*.pdf $toDirectory -Filter *.pdf
                        if(Test-Path $temp_name\*.* -Exclude *.meta, *.pdf)
                        {
                            Write-Host (timestamp) " WARNING/ERROR not all documents have been moved. Only PDFs was moved!"
                            Write-Host (timestamp) " Check folder for other document-types."
                        }
                }
                Move-Item $extractGUIDdir $fromDirectory\_copied
        }

Write-Host (timestamp) " DONE!"

# Stop logging
Stop-Transcript
Run Code Online (Sandbox Code Playgroud)

ola*_*ell 15

它解决了.

它工作,愚蠢我没有按F5任务调度程序中的(更新)来更新gui中任务的状态.

其实我很确定我这样做了,但显然不是.

  • 最愚蠢的是UI只是部分事件驱动的,所以我们会在它启动时自动查看,而不是在它完成时.我在这个问题上浪费了一个小时. (3认同)