Public/Get-ADSIObject.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
function Get-ADSIObject
{
<#
.SYNOPSIS
 This function will query any kind of object in Active Directory
 
.DESCRIPTION
 This function will query any kind of object in Active Directory
 
.PARAMETER SamAccountName
 Specify the SamAccountName of the object.
 This parameter also search in Name and DisplayName properties
 Name and Displayname are alias.
 
.PARAMETER DistinguishedName
 Specify the DistinguishedName of the object your are looking for
  
.PARAMETER Credential
    Specify the Credential to use
  
.PARAMETER $DomainDistinguishedName
    Specify the DistinguishedName of the Domain to query
  
.PARAMETER SizeLimit
    Specify the number of item(s) to output
  
.EXAMPLE
 Get-ADSIObject -SamAccountName Fxcat
 
.EXAMPLE
 Get-ADSIObject -Name DC*
  
.NOTES
 Francois-Xavier Cat
 LazyWinAdmin.com
 @lazywinadm
#>

    
    [CmdletBinding()]
    PARAM (
        [Parameter(ParameterSetName = "SamAccountName")]
        [Alias("Name", "DisplayName")]
        [String]$SamAccountName,
        
        [Parameter(ParameterSetName = "DistinguishedName")]
        [String]$DistinguishedName,
        
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [Alias("Domain", "DomainDN", "SearchRoot", "SearchBase")]
        [String]$DomainDistinguishedName = $(([adsisearcher]"").Searchroot.path),
        
        [Alias("RunAs")]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential = [System.Management.Automation.PSCredential]::Empty,
        
        [Alias("ResultLimit", "Limit")]
        [int]$SizeLimit = '100'
    )
    BEGIN { }
    PROCESS
    {
        TRY
        {
            # Building the basic search object with some parameters
            $Search = New-Object -TypeName System.DirectoryServices.DirectorySearcher -ErrorAction 'Stop'
            $Search.SizeLimit = $SizeLimit
            $Search.SearchRoot = $DomainDistinguishedName
            
            IF ($PSBoundParameters['SamAccountName'])
            {
                $Search.filter = "(|(name=$SamAccountName)(samaccountname=$SamAccountName)(displayname=$samaccountname))"
            }
            IF ($PSBoundParameters['DistinguishedName'])
            {
                $Search.filter = "(&(distinguishedname=$DistinguishedName))"
            }
            IF ($PSBoundParameters['DomainDistinguishedName'])
            {
                IF ($DomainDistinguishedName -notlike "LDAP://*") { $DomainDistinguishedName = "LDAP://$DomainDistinguishedName" }#IF
                Write-Verbose -Message "Different Domain specified: $DomainDistinguishedName"
                $Search.SearchRoot = $DomainDistinguishedName
            }
            IF ($PSBoundParameters['Credential'])
            {
                $Cred = New-Object -TypeName System.DirectoryServices.DirectoryEntry -ArgumentList $DomainDistinguishedName, $($Credential.UserName), $($Credential.GetNetworkCredential().password)
                $Search.SearchRoot = $Cred
            }
            
            foreach ($Object in $($Search.FindAll()))
            {
                # Define the properties
                # The properties need to be lowercase!!!!!!!!
                $Properties = @{
                    "DisplayName" = $Object.properties.displayname -as [string]
                    "Name" = $Object.properties.name -as [string]
                    "ObjectCategory" = $Object.properties.objectcategory -as [string]
                    "ObjectClass" = $Object.properties.objectclass -as [string]
                    "SamAccountName" = $Object.properties.samaccountname -as [string]
                    "Description" = $Object.properties.description -as [string]
                    "DistinguishedName" = $Object.properties.distinguishedname -as [string]
                    "ADsPath" = $Object.properties.adspath -as [string]
                    "LastLogon" = $Object.properties.lastlogon -as [string]
                    "WhenCreated" = $Object.properties.whencreated -as [string]
                    "WhenChanged" = $Object.properties.whenchanged -as [string]
                }
                
                # Output the info
                New-Object -TypeName PSObject -Property $Properties
            }
        }
        CATCH
        {
            Write-Warning -Message "[PROCESS] Something wrong happened!"
            Write-Warning -Message $error[0].Exception.Message
        }
    }
    END
    {
        Write-Verbose -Message "[END] Function Get-ADSIObject End."
    }
}