4
在計劃任務中運行PowerShell腳本時遇到問題。即使腳本已經註銷了最後一行「完成!」,它仍然停留在Running中。而且它看起來也完全符合我的要求。我錯過了什麼?計劃的PowerShell任務,卡住運行
當從Run
在Windows中運行它,它也似乎很動聽:powershell -file "D:\_temp\copy_bat\copy_one_reprint_folder.ps1"
Seetings任務計劃程序是:
- 具有完全特權時
- 運行weither用戶登錄或不 運行
- 動作,啓動程序
powershell.exe
-file "D:\_temp\copy_bat\copy_one_reprint_folder.ps1"
如有需要,請參見下面的代碼。
# 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
最愚蠢的是UI僅僅是部分事件驅動的,所以我們在啓動時會自動看到,而不是在完成時看到。我在這個問題上浪費了一個小時。 – Mc128k 2017-11-26 14:07:36