我正在尝试创建一个脚本,该脚本在运行时将打印位于指定文件夹中的所有PDF。另外,我希望脚本在文件打印后将这些PDF移到“存档”文件夹中以供参考。此外,脚本完成后还需要关闭Adobe。
我可以使文件正确打印,而无需任何移动或取消Adobe进程的尝试,但是,如果我为这两个文件中的任何一个添加脚本,都会得到不成功的结果。
$files = Get-ChildItem “C:\Users\bwhite\Desktop\DocsToPrint\*.pdf”
foreach ($file in $files){
start-process -FilePath $file.fullName -Verb Print -PassThru -Wait
}
Move-Item -Path C:\Users\bwhite\Desktop\DocsToPrint\*.pdf -Destination C:\Users\bwhite\Desktop\DocsToPrint\Archive
答案 0 :(得分:0)
将文件发送到打印机是对后台打印程序的假脱机操作。打印机的速度将确定打印作业是否完成。发送到打印机的速度很快,文件系统已处理完文件,然后移至下一个文件系统。
工作只会在后台打印程序中排队。您不处理任何打印机活动,包括打印错误。在处理另一个文件之前,您需要编写代码以监视打印机作业的成功和失败。
类似以下内容。 (尽管我没有添加任何错误处理-我会留给您) (好吧,那我正在使用.txt文件和Adobe)
有或没有-Wait,它可以按预期在我的系统上工作。当然,如果您不想看到所有输出消息,则将其取出。另外,Adobe将保持打开状态,因此,您需要编写代码将其杀死。
# Get default printer if that is the printer target
$PrinterName = Get-CimInstance -ClassName Win32_Printer |
ForEach { $PSItem | Where Default -eq $true }
Get-ChildItem 'D:\Temp\NewFiles\*.txt' | Select -First 3 |
foreach {
start-process -FilePath $PSItem.FullName -Verb Print -PassThru #-Wait
do
{
# Get all print jobs on the target printer
$PrintJobs = Get-CimInstance -Class Win32_PerfFormattedData_Spooler_PrintQueue |
Where-Object -Property Name -eq $PrinterName.Name |
Select Name, JobsSpooling, Jobs, JobErrors
Write-Warning -Message "Please wait. Printing $($PSItem.Name) now."
}
while ($PrintJobs.Jobs -ne 0)
# Archive the file when the print job is complete
$paramblock = @{
Path = $PSItem.FullName
Destination = 'D:\temp\Archive'
}
Write-Warning -Message "Printing for $($PSItem.Name) complete. Archiving the file."
Move-Item @paramblock -Verbose
}
# Results, with the -wait, lots of message with each file printed.
<#
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
30 5 1072 2044 0.02 19364 1 notepad
WARNING: Please wait. Printing TestFile0.txt now.
WARNING: Please wait. Printing TestFile0.txt now.
WARNING: Please wait. Printing TestFile0.txt now.
...
WARNING: Printing for TestFile0.txt complete. Archiving the file.
VERBOSE: Performing the operation "Move File" on target "Item: D:\Temp\NewFiles\TestFile0.txt Destination: D:\temp\Archive\TestFile0.txt".
29 5 996 1880 0.00 6708 1 notepad
WARNING: Please wait. Printing TestFile1.txt now.
WARNING: Please wait. Printing TestFile1.txt now.
WARNING: Please wait. Printing TestFile1.txt now.
...
WARNING: Please wait. Printing TestFile1.txt now.
WARNING: Printing for TestFile1.txt complete. Archiving the file.
VERBOSE: Performing the operation "Move File" on target "Item: D:\Temp\NewFiles\TestFile1.txt Destination: D:\temp\Archive\TestFile1.txt".
27 4 908 1760 0.03 10108 1 notepad
WARNING: Please wait. Printing TestFile2.txt now.
WARNING: Please wait. Printing TestFile2.txt now.
WARNING: Please wait. Printing TestFile2.txt now.
...
WARNING: Printing for TestFile2.txt complete. Archiving the file.
VERBOSE: Performing the operation "Move File" on target "Item: D:\Temp\NewFiles\TestFile2.txt Destination: D:\temp\Archive\TestFile2.txt".
#>
# Without the -Wait, lot's of message output for the first file printed, only 3 messages for each file there after.
<#
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
30 5 1032 1908 0.02 3704 1 notepad
WARNING: Please wait. Printing TestFile0.txt now.
WARNING: Please wait. Printing TestFile0.txt now.
WARNING: Please wait. Printing TestFile0.txt now.
...
WARNING: Printing for TestFile0.txt complete. Archiving the file.
VERBOSE: Performing the operation "Move File" on target "Item: D:\Temp\NewFiles\TestFile0.txt Destination: D:\temp\Archive\TestFile0.txt".
28 4 936 1788 0.00 13772 1 notepad
WARNING: Please wait. Printing TestFile1.txt now.
WARNING: Printing for TestFile1.txt complete. Archiving the file.
VERBOSE: Performing the operation "Move File" on target "Item: D:\Temp\NewFiles\TestFile1.txt Destination: D:\temp\Archive\TestFile1.txt".
28 4 852 1700 0.00 6196 1 notepad
WARNING: Please wait. Printing TestFile2.txt now.
WARNING: Printing for TestFile2.txt complete. Archiving the file.
VERBOSE: Performing the operation "Move File" on target "Item: D:\Temp\NewFiles\TestFile2.txt Destination: D:\temp\Archive\TestFile2.txt".
#>
为此更新OP。
我尝试进行此更改以适应文件路径和 用于PDF,但是它会立即打印第一个 在我收到第一个文件之前,已收到45942行的写警告 停止了脚本
这些消息只是出于发生什么情况和何时发生的目的。根本不需要它们。
现在,所有这些,听起来很环保。
我之所以这么说是因为,如图所示,我发布的内容行之有效,我通过放置一堆PDF文件进行了第二次测试,尽管我收到了更多消息,因为Adode需要一些时间来完成它的工作并发送东西到打印机,它仍然可以工作。
因此,对于您所遇到的问题,Adobe是问题的根本原因,而不是PowerShell。我创建并拖放到该文件夹中以进行测试的PDF的内容与文本文件中的内容完全相同。
实际结果,而不使用-wait:
<#
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
40 4 744 2216 0.05 18060 2 AcroRd32
WARNING: Please wait. Printing TestFile0.pdf now.
WARNING: Printing for TestFile0.pdf complete. Archiving the file.
VERBOSE: Performing the operation "Move File" on target "Item: D:\Temp\NewFiles\TestFile0.pdf Destination: D:\temp\Archive\TestFile0.pdf".
49 5 796 2488 0.02 7588 2 AcroRd32
WARNING: Please wait. Printing TestFile1.pdf now.
WARNING: Please wait. Printing TestFile1.pdf now.
WARNING: Please wait. Printing TestFile1.pdf now.
WARNING: Please wait. Printing TestFile1.pdf now.
WARNING: Please wait. Printing TestFile1.pdf now.
WARNING: Please wait. Printing TestFile1.pdf now.
WARNING: Please wait. Printing TestFile1.pdf now.
WARNING: Please wait. Printing TestFile1.pdf now.
WARNING: Please wait. Printing TestFile1.pdf now.
WARNING: Please wait. Printing TestFile1.pdf now.
WARNING: Please wait. Printing TestFile1.pdf now.
WARNING: Please wait. Printing TestFile1.pdf now.
WARNING: Please wait. Printing TestFile1.pdf now.
WARNING: Please wait. Printing TestFile1.pdf now.
WARNING: Please wait. Printing TestFile1.pdf now.
WARNING: Printing for TestFile1.pdf complete. Archiving the file.
VERBOSE: Performing the operation "Move File" on target "Item: D:\Temp\NewFiles\TestFile1.pdf Destination: D:\temp\Archive\TestFile1.pdf".
47 5 824 2532 0.00 23528 2 AcroRd32
WARNING: Please wait. Printing TestFile2.pdf now.
WARNING: Printing for TestFile2.pdf complete. Archiving the file.
VERBOSE: Performing the operation "Move File" on target "Item: D:\Temp\NewFiles\TestFile2.pdf Destination: D:\temp\Archive\TestFile2.pdf".
#>
每个文件很小(每页1页-大约6行文本),几乎具有完全相同的内容。通过在打印队列运行时对其进行监视,您将看到每个打印作业要花费多长时间,这并不包括Adobe为移动而释放文件的版本。
PDF越大,每次作业所需的时间就越长。同样,仅将消息用于小样本,而不用于生产运行。记住,每次生成的过程ID都会显示您每次启动Adobe的时间。