Guest User

Untitled

a guest
Feb 10th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.37 KB | None | 0 0
  1. function Get-DSUserByProperty
  2. {
  3. <#
  4. .SYNOPSIS
  5. Search for users in the directory.
  6.  
  7. .DESCRIPTION
  8. Using native System.DirectoryServices, searches the directory (default is to use
  9. the global catalog) for entries that match
  10.  
  11. .PARAMETER TargetDomain
  12. Specifies the domain to run the search against.
  13.  
  14. .PARAMETER UserName
  15. Specifies one or more items identifying users by a single property. Examples could
  16. include a list of users by display name, sam account name, mail, or user principal name.
  17.  
  18. .PARAMETER Property
  19. Specifies the property to search against, from a common set of properties.
  20.  
  21. .PARAMETER PropertyUserDefined
  22. Specifies the property to search against, defined at time of invocation. Property
  23. value will be checked against the schema of the TargetDomain. Using this parameter
  24. may introduce a delay at the beginning of invocation, while the list of indexed
  25. user properties is collected.
  26.  
  27. .PARAMETER UseLDAP
  28. Switch parameter, directs the search to target only the (local) directory, not
  29. the global catalog.
  30.  
  31. .EXAMPLE
  32. PS > Get-DSUserByProperty -UserName [email protected] -Property mail
  33.  
  34. alias : jdoe
  35. displayname : John Doe (Product Dev)
  36. title : Product Development Researcher
  37. userprincipalname : [email protected]
  38. manager : alicesm
  39. managerMail : [email protected]
  40. department : Widget Research
  41.  
  42. Description
  43. -----------
  44. Searching for users that have '[email protected]' as the mail attribute.
  45.  
  46. .EXAMPLE
  47. PS > $listOfUsers = @( "jdoe", "alicesm", "charlesf", "ericalewis")
  48. PS > Get-DSUserByProperty -UserName $listOfUsers -Property samaccountname
  49.  
  50. WARNING: Could not find ericalewis
  51.  
  52. alias : jdoe
  53. displayname : John Doe (Product Dev)
  54. title : Product Development Researcher
  55. userprincipalname : [email protected]
  56. manager : alicesm
  57. managerMail : [email protected]
  58. department : Widget Research
  59.  
  60. alias : alicesm
  61. displayname : Alice Smith (Widget Manager)
  62. title : Widget Manager
  63. userprincipalname : [email protected]
  64. manager : erical
  65. managerMail : [email protected]
  66. department : Adminstration
  67.  
  68. alias : charlesf
  69. displayname : Charles Fox (Internet Janitor)
  70. title : Internet Janitor
  71. userprincipalname : [email protected]
  72. manager : alicesm
  73. managerMail : [email protected]
  74. department : Cloud Sanitation
  75.  
  76. Description
  77. -----------
  78. Given an array of values, searches for each entry using the specified property. In
  79. this example, the entry 'ericalewis' did not match the samaccountname of any user, as
  80. indicated by the warning.
  81.  
  82. .INPUTS
  83. System.String
  84.  
  85. .OUTPUTS
  86. PSCustomObject
  87.  
  88. .LINK
  89. about_comment_based_help
  90.  
  91. .NOTES
  92.  
  93. #### Name: Get-DSUserByProperty
  94. #### Author: J Schell
  95. #### Version: 0.1.1
  96. #### License: MIT License
  97.  
  98. ### Change Log
  99.  
  100. ##### 2017-02-10::0.1.1
  101. -logic fix for results that have more than one object returned.
  102.  
  103. ##### 2017-02-10::0.1.0
  104. -initial creation
  105. -fork/ consolidation of multiple versions of lookup by 'x' property on users
  106.  
  107. #>
  108.  
  109.  
  110. [CmdletBinding(DefaultParameterSetName = "CommonProperty")]
  111. [OutputType([PSCustomObject])]
  112. Param
  113. (
  114. [Parameter(Mandatory = $False,
  115. ParameterSetName = "__AllParameterSets")]
  116. [String]
  117. $TargetDomain = $env:USERDNSDOMAIN,
  118.  
  119. [Parameter(Mandatory = $True,
  120. ParameterSetName = "__AllParameterSets")]
  121. [String[]]
  122. $UserName,
  123.  
  124. [Parameter(Mandatory = $True,
  125. ParameterSetName = "CommonProperty")]
  126. [ValidateSet("samaccountname","displayname","mail","userprincipalname")]
  127. [String]
  128. $Property,
  129.  
  130. [Parameter(Mandatory = $True,
  131. ParameterSetName = "UserDefinedProperty")]
  132. [String]
  133. $PropertyUserDefined,
  134.  
  135. [Parameter(Mandatory = $False)]
  136. [Switch]
  137. $UseLDAP
  138. )
  139.  
  140. Begin
  141. {
  142. $DomainContext = [System.DirectoryServices.ActiveDirectory.DirectoryContext]::New("Domain", $TargetDomain)
  143. Try
  144. {
  145. $DomainEntry = [System.DirectoryServices.ActiveDirectory.Domain]::GetDomain($DomainContext)
  146. }
  147. Catch
  148. {
  149. Write-Error $_
  150. Break
  151. }
  152.  
  153. if( $PropertyUserDefined )
  154. {
  155. $ForestContext = [System.DirectoryServices.ActiveDirectory.DirectoryContext]::New("Forest", $($DomainEntry.Forest.Name) )
  156.  
  157. $Schema = [System.DirectoryServices.ActiveDirectory.ActiveDirectorySchema]::GetSchema($ForestContext)
  158. $userMandatoryProperties = @( $Schema.FindClass("User").MandatoryProperties |
  159. Where-Object {$_.isIndexed -eq $True} |
  160. Select-Object -ExpandProperty Name )
  161. $userOptionalProperties = @( $Schema.FindClass("User").OptionalProperties |
  162. Where-Object {$_.isIndexed -eq $True} |
  163. Select-Object -ExpandProperty Name )
  164.  
  165. $Schema.Dispose()
  166.  
  167. $userProperties = @( $userMandatoryProperties )
  168. $userProperties += @( $userOptionalProperties )
  169.  
  170. $msgUserPropertiesIndexedFoundInSchema = "Properties found: $($userProperties.count)"
  171. Write-Verbose $msgUserPropertiesIndexedFoundInSchema
  172.  
  173. if( $userProperties -contains $PropertyUserDefined)
  174. {
  175.  
  176. $PropertyToSearch = $PropertyUserDefined
  177. }
  178. else
  179. {
  180. $msgPropertyUserDefinedNotInSchema = "The property `'$($PropertyUserDefined)`'' " +
  181. "was not found as a property for the user class in the schema."
  182. Write-Error $msgPropertyUserDefinedNotInSchema
  183. Break
  184. }
  185. }
  186. else
  187. {
  188. $PropertyToSearch = $Property
  189. }
  190. Write-Output "Search on: $($PropertyToSearch)"
  191.  
  192. if($UseLDAP)
  193. {
  194. $TargetSearch = "LDAP://$($DomainEntry.Name):389"
  195. }
  196. else
  197. {
  198. $Target = "GC://$($DomainEntry.Name):3268"
  199. }
  200. $DomainEntry.Dispose()
  201.  
  202. $propertiesOfInterest = @(
  203. "alias"
  204. "displayname"
  205. "mail"
  206. "title"
  207. "department"
  208. "userprincipalname"
  209. "manager"
  210. "managerMail"
  211. )
  212. }
  213. Process
  214. {
  215. $UsersFound = @()
  216. $MissingUsers = @()
  217.  
  218. foreach($User in $UserName)
  219. {
  220. $adsiTarget = [adsi]$Target
  221. $Searcher = [adsisearcher]($adsiTarget)
  222. $ldapFilter = "(&(objectClass=user)($PropertyToSearch=$User))"
  223. $Searcher.Filter = $ldapFilter
  224. $SearchResult = $Searcher.FindAll()
  225.  
  226. if( $($SearchResult.Count) -ge 1)
  227. {
  228. foreach($Result in $SearchResult)
  229. {
  230. if( $($Result.Properties.manager) )
  231. {
  232. $UserManagerPath = [ADSI]"LDAP://$($Result.Properties.manager)"
  233. $UserManagerAlias = $($UserManagerPath.Properties.samaccountname)
  234. $UserManagerMail = $($UserManagerPath.Properties.mail)
  235. }
  236. else
  237. {
  238. $UserManagerAlias = "UnDef"
  239. $UserManagerMail = ""
  240. }
  241. $UserFound = New-Object -TypeName PsObject -Property ([ordered]@{
  242. samaccountname = $($Result.Properties.samaccountname)
  243. displayname = $($Result.Properties.displayname)
  244. mail = $($Result.Properties.mail)
  245. title = $($Result.Properties.title)
  246. department = $($Result.Properties.department)
  247. userprincipalname = $($Result.Properties.userprincipalname)
  248. manager = $UserManagerAlias
  249. managerMail = $UserManagerMail
  250. })
  251. $UsersFound += @( $UserFound )
  252. }
  253. }
  254. else
  255. {
  256. $MissingUsers += @( $User )
  257. Write-Warning "Could not find $($User)"
  258. }
  259. $Searcher.Dispose()
  260. }
  261. }
  262. End
  263. {
  264. $UsersFound
  265. }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment