# requires Windows 10 / Windows Server 2016 or better
# choose a name for your new printer $printerName = 'PrintPDFUnattended' # choose a default path where the PDF is saved $PDFFilePath = "$env:temp\PDFResultFile.pdf" # choose whether you want to print a test page $TestPage = $true
# see whether the driver exists $ok = @(Get-PrinterDriver-Name"Microsoft Print to PDF"-ea0).Count -gt0 if (!$ok) { Write-Warning"Printer driver 'Microsoft Print to PDF' not available." Write-Warning"This driver ships with Windows 10 or Windows Server 2016." Write-Warning"If it is still not available, enable the 'Printing-PrintToPDFServices-Features'" Write-Warning"Example: Enable-WindowsOptionalFeature -Online -FeatureName Printing-PrintToPDFServices-Features" return }
# check whether port exists $port = Get-PrinterPort-Name$PDFFilePath-ErrorAction SilentlyContinue if ($port-eq$null) { # create printer port Add-PrinterPort-Name$PDFFilePath }
# add printer Add-Printer-DriverName"Microsoft Print to PDF"-Name$printerName-PortName$PDFFilePath
# print a test page to the printer if ($TestPage) { $printerObject = Get-CimInstance Win32_Printer -Filter"name LIKE '$printerName'" $null = $printerObject | Invoke-CimMethod-MethodName printtestpage Start-Sleep-Seconds1 Invoke-Item-Path$PDFFilePath }
Windows 10 和 Windows Server 2016 终于带来了内置的 PDF 打印机,名为 “Microsoft Print to PDF”。您可以在 PowerShell 中用它来创建 PDF 文件。请运行这段代码来测试您的 PDF 打印机:
1 2 3 4 5 6 7 8 9
$printer = Get-Printer-Name"Microsoft Print to PDF"-ErrorAction SilentlyContinue if (!$?) { Write-Warning"Your PDF Printer is not yet available!" } else { Write-Warning"PDF printer is ready for use." }
如果您的打印机不能用(或是暂时不能用),那么您可能使用的不是 Windows 10 或 Windows Server 2016,或者 PDF 打印功能尚未启用。请在管理员权限下运行 PowerShell 并执行这段命令来修复它:
# make sure the watcher emits events $FileSystemWatcher.EnableRaisingEvents = $true
# define the code that should execute when a file change is detected $Action = { $details = $event.SourceEventArgs $Name = $details.Name $FullPath = $details.FullPath $OldFullPath = $details.OldFullPath $OldName = $details.OldName $ChangeType = $details.ChangeType $Timestamp = $event.TimeGenerated $text = "{0} was {1} at {2}"-f$FullPath, $ChangeType, $Timestamp Write-Host"" Write-Host$text-ForegroundColor Green
# you can also execute code based on change type here switch ($ChangeType) { 'Changed' { "CHANGE" } 'Created' { "CREATED"} 'Deleted' { "DELETED" # uncomment the below to mimick a time intensive handler <# Write-Host "Deletion Handler Start" -ForegroundColor Gray Start-Sleep -Seconds 4 Write-Host "Deletion Handler End" -ForegroundColor Gray #> } 'Renamed' { # this executes only when a file was renamed $text = "File {0} was renamed to {1}"-f$OldName, $Name Write-Host$text-ForegroundColor Yellow } default { Write-Host$_-ForegroundColor Red -BackgroundColor White } } }
Write-Host"Monitoring content of $PathToMonitor" explorer $PathToMonitor while ($true) { $Change = $FileSystemWatcher.WaitForChanged('All', 1000) if ($Change.TimedOut -eq$false) { # get information about the changes detected Write-Host"Change detected:" $Change | Out-Default
# uncomment this to see the issue #Start-Sleep -Seconds 5 } else { Write-Host"."-NoNewline } }
这个示例可以正常工作。当您向监控的文件夹增加文件,或者作出改变时,将会监测到改变的类型。您可以容易地得到该信息并采取操作。例如,对于 IT 部门,人们可以向一个投放文件夹投放文件和说明,您的脚本可以自动处理这些文件。
# set the event log name you want to subscribe to # (use Get-EventLog -AsString for a list of available event log names) $Name = 'Application'
# get an instance $Log = [System.Diagnostics.EventLog]$Name
# determine what to do when an event occurs $Action = { # get the original event entry that triggered the event $entry = $event.SourceEventArgs.Entry
# log all events Write-Host"Received from $($entry.Source): $($entry.Message)"
# do something based on a specific event if ($entry.EventId -eq1-and$entry.Source -eq'WinLogon') { Write-Host"Test event was received!"-ForegroundColor Red }
}
# subscribe to its "EntryWritten" event $job = Register-ObjectEvent-InputObject$log-EventName EntryWritten -SourceIdentifier'NewEventHandler'-Action$Action
# now whenever an event is written to the log, $Action is executed # use a loop to keep PowerShell busy. You can abort via CTRL+C
Write-Host"Listening to events"-NoNewline
try { do { Wait-Event-SourceIdentifier NewEventHandler -Timeout1 Write-Host"."-NoNewline
} while ($true) } finally { # this executes when CTRL+C is pressed Unregister-Event-SourceIdentifier NewEventHandler Remove-Job-Name NewEventHandler Write-Host"" Write-Host"Event handler stopped." }
Simply “convert” the event log name into an object of “EventLog” type. The result looks similar to this and provides information about the number of entries and the log file size: 只需要将时间日志名称“转换”为一个 “EventLog“ 类型的对象。结果类似这样,并且提供了条目的数量和日志文件尺寸等信息:
1 2 3 4 5
PS> $systemLogDirect
Max(K) Retain OverflowAction Entries Log ------------------------------------ 20.4800 OverwriteAsNeeded 19.806 System