Last active
August 29, 2015 14:18
-
-
Save megamorf/c1e93dd3abaf33c51e84 to your computer and use it in GitHub Desktop.
Powershell Printer monitoring - redundant AD query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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