You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需持续运行、可跨服务器重启的PowerShell PDF自动触发打印方案咨询

解决方案:无持续运行/无任务计划的PDF自动打印触发

针对监控PDF创建、自动打印且重启后无需手动恢复的需求,以下是两种可行方案:


方案1:永久WMI事件订阅(实时触发,推荐)

WMI的事件订阅存储在系统CIM仓库中,服务器重启后不会丢失,能在新PDF创建时实时触发脚本,无需脚本持续运行。

配置步骤(需管理员权限运行PowerShell)

  1. 定义监控文件夹和脚本路径变量:
$targetFolder = "C:\Your\Target\Folder"  # 替换为你的监控文件夹路径
$scriptPath = "C:\Path\To\Your\PrintScript.ps1"  # 替换为你的打印脚本路径
  1. 创建WMI事件过滤器(监控PDF文件创建):
$filterQuery = @"
SELECT * FROM __InstanceCreationEvent WITHIN 10
WHERE TargetInstance ISA 'CIM_DirectoryContainsFile'
AND TargetInstance.GroupComponent = 'Win32_Directory.Name="$($targetFolder.Replace('\','\\'))"'
AND TargetInstance.PartComponent LIKE '%\.pdf'
"@

$filter = New-CimInstance -Namespace root/subscription -ClassName __EventFilter -Property @{
    Name = "PDFCreationFilter"
    EventNameSpace = "root/cimv2"
    QueryLanguage = "WQL"
    Query = $filterQuery
}
  1. 创建事件消费者(调用你的打印脚本):
$consumer = New-CimInstance -Namespace root/subscription -ClassName CommandLineEventConsumer -Property @{
    Name = "PDFPrintConsumer"
    ExecutablePath = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
    CommandLineTemplate = "-NoProfile -ExecutionPolicy Bypass -File `"$scriptPath`" -FilePath `"%TargetInstance.PartComponent%`""
}
  1. 绑定过滤器与消费者:
$binding = New-CimInstance -Namespace root/subscription -ClassName __FilterToConsumerBinding -Property @{
    Filter = [ref]$filter
    Consumer = [ref]$consumer
}

取消订阅(如需)

# 删除过滤器
Get-CimInstance -Namespace root/subscription -ClassName __EventFilter -Filter "Name='PDFCreationFilter'" | Remove-CimInstance
# 删除消费者
Get-CimInstance -Namespace root/subscription -ClassName CommandLineEventConsumer -Filter "Name='PDFPrintConsumer'" | Remove-CimInstance
# 删除绑定关系
Get-CimInstance -Namespace root/subscription -ClassName __FilterToConsumerBinding -Filter "Filter='__EventFilter.Name=""PDFCreationFilter""'" | Remove-CimInstance

注意事项

  • 你的打印脚本需支持通过-FilePath参数接收新创建的PDF路径,执行打印逻辑(例如用Start-Process -Verb Print)
  • 确保PowerShell执行策略允许运行脚本(可临时用-ExecutionPolicy Bypass,或永久设置为RemoteSigned)

方案2:注册表启动项+开机扫描(非实时,简单易配置)

通过将扫描脚本添加到系统启动项,每次服务器重启后自动运行一次,扫描并打印上次重启后新增的PDF文件。

步骤1:修改打印脚本,添加扫描逻辑

$watchFolder = "C:\Your\Target\Folder"
$processedFolder = "$watchFolder\Processed"
$lastRunLog = "$watchFolder\LastRun.txt"

# 初始化必要目录和日志
if (-not (Test-Path $processedFolder)) { New-Item -ItemType Directory -Path $processedFolder | Out-Null }
if (-not (Test-Path $lastRunLog)) { (Get-Date).AddDays(-1) | Out-File $lastRunLog }

# 获取上次运行时间,筛选新创建的PDF
$lastRun = Get-Content $lastRunLog | Get-Date
$newPDFs = Get-ChildItem -Path $watchFolder -Filter *.pdf -File | Where-Object { $_.CreationTime -gt $lastRun }

# 打印并标记已处理
foreach ($pdf in $newPDFs) {
    Start-Process -FilePath $pdf.FullName -Verb Print -Wait
    Move-Item -Path $pdf.FullName -Destination $processedFolder -Force
}

# 更新最后运行时间
(Get-Date) | Out-File $lastRunLog

步骤2:添加系统启动项(管理员权限)

将脚本注册到HKLM启动项,确保重启后自动运行:

$regPath = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Run"
$scriptPath = "C:\Path\To\Your\ScanAndPrintScript.ps1"
Set-ItemProperty -Path $regPath -Name "PDFPrintScanner" -Value "powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"$scriptPath`""

优缺点

  • 优点:配置简单,无需复杂的WMI操作
  • 缺点:仅在开机时扫描一次,无法实时触发,适合批量生成PDF的场景

内容的提问来源于stack exchange,提问作者OdinData

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 01:23:11