Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save megamorf/c1e93dd3abaf33c51e84 to your computer and use it in GitHub Desktop.
Save megamorf/c1e93dd3abaf33c51e84 to your computer and use it in GitHub Desktop.
Powershell Printer monitoring - redundant AD query
# in reference to: https://iamsquib.wordpress.com/2015/03/27/print-monitoring-with-powershell/
# bad
Get-WinEvent -Logname='Microsoft-Windows-PrintService/Operational' -Computername printserver | where{$_.userid -eq 307 } | select TimeCreated, ID, UserId, Message
# returns all contents of the Operational log from the remote server and filters them locally
# better
Get-WinEvent -FilterHashtable @{logname='Microsoft-Windows-PrintService/Operational'; id=307} -Computername printserver | select TimeCreated, ID, UserId, Message
# returns filtered results from remote server, no further filtering needed
# in reference to: https://iamsquib.wordpress.com/2015/03/27/print-monitoring-with-powershell/
# Bad
$user = Get-ADUser $log.UserId -properties Department | select -expandproperty Name
$department = Get-ADUser $log.UserId -properties Department | select -ExpandProperty Department
# You don't have to expand the values when you simply access them via the dot notation: $object.property (see example below)
# Better
$user = Get-ADUser $log.UserId -properties Department
$username = $user.name
$department = $user.department
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment