首页 > 解决方案 > 在 Windows Server 中自动启动我自己的脚本

问题描述

几个客户端使用扫描仪并将 .tiff 文件发送到共享文件夹。此扫描仪无法将这些结果转换为 pdf。如何启动我的脚本以自动将此 .tiff 文件传输到 .pdf?我在 Windows Server 2012 中有哪些变体?如果此脚本在每次创建新的 .tiff 文件后都能正常工作,那就更好了。

标签: windowspowershell

解决方案


监视服务器目录中的文件更改

开始监控.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')
}

推荐阅读