Created
September 25, 2023 18:55
-
-
Save joshfinley/15b062ecc320d92d565ffbcf90b288d2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Import the Active Directory module if not already loaded | |
Import-Module ActiveDirectory | |
# Create a script block to process the piped input | |
filter CheckADUser { | |
# Assume $_ is the object passed from the pipeline, attempt to select the Name property | |
$name = $_.Name | |
# If Name property is found, check the AD | |
if ($null -ne $name) { | |
# Attempt to get the user from AD | |
$user = Get-ADUser -Filter { Name -eq $name } -ErrorAction SilentlyContinue | |
# If user is found, check if it's a human user ID based on some criteria (e.g., DistinguishedName does not contain 'ServiceAccounts') | |
if ($null -ne $user) { | |
if ($user.DistinguishedName -notmatch 'ServiceAccounts') { | |
$user | |
} | |
} | |
} | |
} | |
# Example Usage: | |
# YourPreviousCommand | Format-List | CheckADUser |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment