Last active
June 19, 2018 18:55
-
-
Save AndySchneiderDev-zz/2b9521e1db5d9a582d23 to your computer and use it in GitHub Desktop.
PowerShell to Udpate Active Directory Schema
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
# This should only be used in a development environment and is for demonstration purposes only | |
# Use at your own risk | |
# This adds an attribute to the user class | |
# Written by Andy Schneider, http://get-powershell.com | |
Function New-OID { | |
$Prefix="1.2.840.113556.1.8000.2554" | |
$GUID=[System.Guid]::NewGuid().ToString() | |
$Parts=@() | |
$Parts+=[UInt64]::Parse($guid.SubString(0,4),"AllowHexSpecifier") | |
$Parts+=[UInt64]::Parse($guid.SubString(4,4),"AllowHexSpecifier") | |
$Parts+=[UInt64]::Parse($guid.SubString(9,4),"AllowHexSpecifier") | |
$Parts+=[UInt64]::Parse($guid.SubString(14,4),"AllowHexSpecifier") | |
$Parts+=[UInt64]::Parse($guid.SubString(19,4),"AllowHexSpecifier") | |
$Parts+=[UInt64]::Parse($guid.SubString(24,6),"AllowHexSpecifier") | |
$Parts+=[UInt64]::Parse($guid.SubString(30,6),"AllowHexSpecifier") | |
$oid=[String]::Format("{0}.{1}.{2}.{3}.{4}.{5}.{6}.{7}",$prefix,$Parts[0], | |
$Parts[1],$Parts[2],$Parts[3],$Parts[4],$Parts[5],$Parts[6]) | |
$oid | |
} | |
Function Update-Schema { | |
[CmdletBinding(SupportsShouldProcess,ConfirmImpact='High')] | |
param( | |
[Parameter(Mandatory,ValueFromPipelinebyPropertyName)] | |
$Name, | |
[Parameter(Mandatory,ValueFromPipelinebyPropertyName)] | |
[Alias('DisplayName')] | |
$LDAPDisplayName, | |
[Parameter(Mandatory,ValueFromPipelinebyPropertyName)] | |
[Alias('Description')] | |
$AdminDescription, | |
[Parameter(Mandatory,ValueFromPipelinebyPropertyName)] | |
[Alias('SingleValued')] | |
$IsSingleValued, | |
[Parameter(ValueFromPipelinebyPropertyName)] | |
[Alias('OID')] | |
$AttributeID = (New-OID) | |
) | |
BEGIN {} | |
PROCESS { | |
$schemaPath = (Get-ADRootDSE).schemaNamingContext | |
$type = 'attributeSchema' | |
switch ($isSingleValued) | |
{ | |
'True' {$IsSingleValued = $true} | |
'False' {$IsSingleValued = $false} | |
default {$IsSingleValued = $true} | |
} | |
$attributes = @{ | |
lDAPDisplayName = $Name; | |
attributeId = $AttributeID; | |
oMSyntax = 20; | |
attributeSyntax = "2.5.5.4"; | |
isSingleValued = $IsSingleValued; | |
adminDescription = $AdminDescription; | |
searchflags = 1 | |
} | |
$ConfirmationMessage = "$schemaPath. This cannot be undone" | |
$Caption = 'Updating Active Directory Schema' | |
if ($PSCmdlet.ShouldProcess($ConfirmationMessage,$Caption)) | |
{ | |
New-ADObject -Name $Name -Type $type -Path $schemapath -OtherAttributes $attributes | |
$userSchema = get-adobject -SearchBase $schemapath -Filter 'name -eq "user"' | |
$userSchema | Set-ADObject -Add @{mayContain = $Name} | |
} | |
} | |
END {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment