Skip to content

Instantly share code, notes, and snippets.

@santisq
Last active April 24, 2024 20:30
Show Gist options
  • Save santisq/c1abd1b0426d469b09f4cf2d0e0ec797 to your computer and use it in GitHub Desktop.
Save santisq/c1abd1b0426d469b09f4cf2d0e0ec797 to your computer and use it in GitHub Desktop.
Import-Module ActiveDirectory
$func = Invoke-RestMethod https://gist.githubusercontent.com/santisq/bd3d1d47c89f030be1b4e57b92baaddd/raw/73952eceb82cd31b8cb9f2fd04e2a3ef3c428110/Measure-Expression.ps1
. ([scriptblock]::Create($func))
$adsi = {
$searcher = [adsisearcher]::new(
'(&(objectClass=user)(objectCategory=person))',
[string[]] @(
'DistinguishedName'
'userAccountControl'
'GivenName'
'Name'
'structuralObjectClass'
'ObjectGUID'
'SamAccountName'
'ObjectSID'
'sn'
'UserPrincipalName'))
class AdsiUser {
[string] $DistinguishedName
[bool] $Enabled
[string] $GivenName
[string] $Name
[string] $ObjectClass
[guid] $ObjectGUID
[string] $SamAccountName
[System.Security.Principal.SecurityIdentifier] $SID
[string] $Surname
[string] $UserPrincipalName
AdsiUser([System.DirectoryServices.SearchResult] $searchresult) {
$properties = $searchresult.Properties
$this.DistinguishedName = $properties['distinguishedname'][0]
$this.Enabled = ($properties['userAccountControl'][0] -band 2) -eq 0
$this.GivenName = $properties['givenname'][0]
$this.Name = $properties['name'][0]
$this.ObjectClass = @($properties['structuralobjectclass'])[-1]
$this.ObjectGUID = [guid]::new($properties['objectGuid'][0])
$this.SamAccountName = $properties['samaccountname'][0]
$this.SID = [System.Security.Principal.SecurityIdentifier]::new($properties['ObjectSID'][0], 0)
$this.Surname = $properties['sn'][0]
$this.UserPrincipalName = $properties['userprincipalname'][0]
}
}
foreach ($user in $searcher.FindAll()) {
[AdsiUser]::new($user)
}
}
time -TestCount 5 -OutputAllTests @{
adsisearcher = $adsi
admodule = { Get-ADUser -Filter * }
}
@santisq
Copy link
Author

santisq commented Apr 24, 2024

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment