![Windows Server 2016 Automation with PowerShell Cookbook(Second Edition)](https://wfqqreader-1252317822.image.myqcloud.com/cover/733/36700733/b_36700733.jpg)
上QQ阅读APP看书,第一时间看更新
How to do it...
- Run webtutil to turn on printer monitoring:
$log = 'Microsoft-Windows-PrintService'
webtutil.exe sl $log /operational /Enabled:true
- Define a Function:
- Specify the Function header for an advanced function:
Function Get-PrinterUsage { [CmdletBinding()] Param()
-
- Get the events from the PrintService event log:
$Dps = Get-WinEvent -LogName
Microsoft-Windows-PrintService/Operational | Where-Object ID -eq 307
-
- Create a hash table for each event log record:
Foreach ($Dp in $Dps) { $Document = [Ordered] @{}
-
- Populate the hash table with properties from the event log entry:
$Document.Id = $dp.Properties[0].value $Document.Type = $dp.Properties[1].value $Document.User = $dp.Properties[2].value $Document.Computer = $dp.Properties[3].value $Document.Printer = $dp.Properties[4].value $Document.Port = $dp.Properties[5].value $Document.Bytes = $dp.Properties[6].value $Document.Pages = $dp.Properties[7].value
-
- Create an object for this printer usage entry:
$UEntry = New-Object -Type PSObject
-Property $Document
-
- Give it a better type name:
$UEntry.PsTypeNames.Clear() $UEntry.PsTypeNames.Add("Packt.PrintUsage")
-
- Output the entry:
$UEntry } # End of foreach } # End of function
- Set and use an alias to get the printer usage:
Set-Alias -Name GPRU
-Value Get-PrinterUsage GPRU | Format-Table