Powershell:自動列印PDF

透過排程去呼叫Powershell自動列印特定資料夾的PDF。
第一個是簡易版的

$adobe='C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\acrord32.exe'
$printername='TWTPENP22501'
$Directory = "D:\Test\"
$PrintedDir = "D:\Printed\"

foreach($File in Get-ChildItem -Path $Directory -Recurse)
{

    Move-Item -Path "$Directory$File" -Destination $PrintedDir -Force
    $arglist='/S /T "{0}" "{1}"' -f "$PrintedDir$File", $printername
    start-Process $adobe -ArgumentList $arglist
}
$sleepcount = $CountFiles.Count + [int]11
Start-Sleep $sleepcount

Remove-Item $PrintedDir\*.pdf -Force -ErrorAction SilentlyContinue

第二個是加入Log的

$adobe='C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\acrord32.exe'
$printername='TWTPENP22501'
$Directory = "D:\Test\"
$PrintedDir = "D:\Printed\"
Function LogWrite
{
    Param([string]$logstring)
    $Logfile = "$PrintedDir"+(Get-date -Format 'yyyy-MM-dd')+".txt"   #LogFile
    Add-content $Logfile "$(Get-date -Format 'yyyy-MM-dd tthh:mm:ss') $logstring" 
}
$CountFiles = Get-ChildItem -Path $Directory | measure
foreach($File in Get-ChildItem -Path $Directory -Recurse)
{
    Move-Item -Path "$Directory$File" -Destination $PrintedDir -Force
    LogWrite "Move $Directory$File to $PrintedDir" 
    $arglist='/S /T "{0}" "{1}"' -f "$PrintedDir$File", $printername
    LogWrite "$arglist"
    start-Process $adobe -ArgumentList $arglist
    LogWrite "Print $File"
}
$Countfilenumber = $CountFiles.Count
$sleepcount = $CountFiles.Count + [int]11
LogWrite "Wait $sleepcount seconds to delete file."
Start-Sleep $sleepcount

foreach($WillDelFile in Get-ChildItem -Path $PrintedDir -Recurse)
{
    LogWrite "$WillDelFile will be delete."
} 
Remove-Item $PrintedDir\*.pdf -Force -ErrorAction SilentlyContinue
LogWrite "There are $Countfilenumber PDF files deleted"

準備列印

送出完成

Log紀錄

Adobe程式位置和印表機名稱請自行修改。
放入排成可以讓他定時檢查