Created
          August 15, 2019 19:59 
        
      - 
      
- 
        Save noahpeltier/3070bb8065ce7c6f26661a69e42dbb5c to your computer and use it in GitHub Desktop. 
    Class and functions for searching and settin guser info in ADSI withou tthe need for RSAT
  
        
  
    
      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
    
  
  
    
  | using namespace System | |
| using namespace System.DirectoryServices | |
| class ADSearch | |
| { | |
| [Searchresult]$object | |
| [DirectoryEntry]$DirectoryEntry | |
| [PSCustomObject]$properties | |
| $UserAccountControl | |
| ADSearch($Property,$string) | |
| { | |
| $this.object = ([DirectorySearcher]"(&(objectCategory=person)(objectClass=User)($Property=$string))").FindOne() | |
| $this.DirectoryEntry = $this.object.GetDirectoryEntry() | |
| $this.UserAccountControl = $this.DirectoryEntry.userAccountControl.value | |
| $Enumerator = [pscustomobject]($this.DirectoryEntry.Properties.getenumerator() | Select PropertyName,Value) | |
| $Table = @{} | |
| foreach ($item in $Enumerator) | |
| { | |
| $Table[$item.PropertyName] = $item.Value | |
| } | |
| $Table['AccountDisabled'] = (($this.UserAccountControl -band 0x2) -as [boolean]) | |
| $Table['Unlocked'] = (($this.UserAccountControl -band 0x8388608) -as [boolean]) | |
| $Table['ObjectSID'] = ( new-object System.Security.Principal.SecurityIdentifier($this.DirectoryEntry.objectsid.value,0)).Value | |
| $Table['PasswordLastSet'] = ([datetime]::FromFileTime($this.DirectoryEntry.ConvertLargeIntegerToInt64($this.DirectoryEntry.Properties.pwdLastSet.value))) | |
| $this.properties = [PSCustomObject]$Table | |
| } | |
| [void] Set($property,$value) | |
| { | |
| $this.DirectoryEntry.$property = $value | |
| $this.DirectoryEntry.CommitChanges() | |
| } | |
| Get() | |
| { | |
| $this.Properties | select Name,givenname,sn,distinguishedname,Samaccountname,mail,userprincipalname,PasswordLastSet,AccountDisabled,telephonenumber,objectguid,objectsid,info | |
| } | |
| } | |
| Function Get-ADSIUser | |
| { | |
| [CmdletBinding()] | |
| param | |
| ( | |
| [string]$string, | |
| [ValidateSet('samaccounname','name','mail','userprincipalname')] | |
| [string]$filter = 'samaccountname', | |
| [switch]$AllProperties = $false | |
| ) | |
| $user = [ADSearch]::new($filter,$string) | |
| if (!$AllProperties) | |
| { | |
| return $user.Properties | select Name,givenname,sn,distinguishedname,Samaccountname,mail,userprincipalname,PasswordLastSet,AccountDisabled,telephonenumber,objectguid,objectsid,info | |
| } | |
| else | |
| { | |
| return $user.properties | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment