windows - 在 Windows Server 中自动启动我自己的脚本
问题描述
几个客户端使用扫描仪并将 .tiff 文件发送到共享文件夹。此扫描仪无法将这些结果转换为 pdf。如何启动我的脚本以自动将此 .tiff 文件传输到 .pdf?我在 Windows Server 2012 中有哪些变体?如果此脚本在每次创建新的 .tiff 文件后都能正常工作,那就更好了。
解决方案
监视服务器目录中的文件更改
开始监控.ps1
### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "D:\source"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action = { $path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
$logline = "$(Get-Date), $changeType, $path"
Add-content "D:\log.txt" -value $logline
}
### DECIDE WHICH EVENTS SHOULD BE WATCHED
Register-ObjectEvent $watcher "Created" -Action $action
Register-ObjectEvent $watcher "Changed" -Action $action
Register-ObjectEvent $watcher "Deleted" -Action $action
Register-ObjectEvent $watcher "Renamed" -Action $action
while ($true) {sleep 5}
如何使用
Create a new text file
Copy & paste the above code
Change the following settings to your own needs:
folder to monitor: $watcher.Path = "D:\source"
file filter to include only certain file types: $watcher.Filter = "*.*"
include subdirectories yes/no: $watcher.IncludeSubdirectories = $true
Save and rename it to StartMonitoring.ps1
Start monitoring by Right click » Execute with PowerShell
要停止监控,关闭 PowerShell 窗口就足够了
从 .tiff 转换为 PDF 尝试:
$InputLocation = "C:\convert"
$tool = 'C:Program Files (x86)\PDFCreator\PDFCreator.exe'
$tiffs = get-childitem -filter *.tif -path $InputLocation
foreach($tiff in $tiffs)
{
$filename = $tiff.FullName
$pdf = $tiff.FullName.split('.')[0] + '.pdf'
'Processing ' + $filename + ' to ' + $pdf
$param = "-sOutputFile=$pdf"
Start-Process $tool -ArgumentList ('/IF"' + $filename + '" /OF"' + $pdf + '/NoPSCheck /NoStart')
}
推荐阅读
- python - 与字典比较后有条件地填充列
- dns - Ethernet-to-I2C box not seen when VPN turned on
- node.js - 项目中的猫鼬聚合查找
- regex - Powershell替换cron计划中的第二项?
- android-gradle-plugin - How to configure external build per build variant?
- python - How do I change the same string within a column and make it permanent using Pandas
- flutter - 正确的凭证数据后颤振自动填充触发器
- node.js - 当我使用地理编码器时,我的中间件不起作用
- maven - cxf-xjc-plugin maven 错误“懦弱地拒绝写入不存在的目录“src\main\java””
- java - 是否可以在 ControllerAdvice 中有多个 ExceptionHandler 用于相同的异常但不同的 Web 请求?