Public/Get-ADSISchema.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
function Get-ADSISchema
{
    <#
 .SYNOPSIS
  The Get-ADSISchema function gather information about the current Active Directory Schema
  
 .DESCRIPTION
  The Get-ADSISchema function gather information about the current Active Directory Schema
  
 .PARAMETER PropertyType
  Specify the type of property to return
  
 .PARAMETER ClassName
  Specify the name of the Class to retrieve
  
 .PARAMETER AllClasses
  This will list all the property present in the domain
  
 .PARAMETER FindClassName
  Specify the exact or partial name of the class to search
  
 .EXAMPLE
  Get-ADSISchema -PropertyType Mandatory -ClassName user
  
 .NOTES
  Francois-Xavier Cat
  LazyWinAdmin.com
  @lazywinadm
#>

    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param
    (
        [Parameter(ParameterSetName = 'Default',
                   Mandatory = $true)]
        [ValidateSet("mandatory", "optional")]
        [String]$PropertyType,
        
        [Parameter(ParameterSetName = 'Default',
                   Mandatory = $true)]
        [String]$ClassName,
        
        [Parameter(ParameterSetName = 'AllClasses',
                   Mandatory = $true)]
        [Switch]$AllClasses,
        
        [Parameter(ParameterSetName = 'FindClasses',
                   Mandatory = $true)]
        [String]$FindClassName
    )
    
    BEGIN
    {
        TRY
        {
            $schema = [DirectoryServices.ActiveDirectory.ActiveDirectorySchema]::GetCurrentSchema()
            
        }
        CATCH { }
    }
    
    PROCESS
    {
        IF ($PSBoundParameters['AllClasses'])
        {
            $schema.FindAllClasses().Name
        }
        IF ($PSBoundParameters['FindClassName'])
        {
            $schema.FindAllClasses() | Where-Object { $_.name -match $FindClassName } | Select-Object -Property Name
        }
        
        ELSE
        {
            
            Switch ($PropertyType)
            {
                "mandatory"
                {
                    ($schema.FindClass("$ClassName")).MandatoryProperties
                }
                "optional"
                {
                    ($schema.FindClass("$ClassName")).OptionalProperties
                }
            }#Switch
        }#ELSE
        
    }#PROCESS
}