New-Variable -Name LdapRoot -Visibility Private -Option Readonly -value ([String]::Concat("LDAP://" ,([adsi]"LDAP://RootDSE").defaultNamingContext)) -Scope Global
Function Global:New-LdapPath
{
$object = New-Object Management.Automation.PSObject
Add-Member -in $object ScriptProperty Root {$LdapRoot}{Throw "Root readonly"}
Add-Member -in $object NoteProperty Input ""
Add-Member -in $object ScriptProperty Path `
{
$path = $this.Root
$value = $this.Input
If ($value )
{
If ($value -is [string])
{
$value = $value.Trim()
If ($value.StartsWith("LDAP://")){$path = $value }
Else { If ($value) {$path = "LDAP://" + $value } }
}
Else { If ($value.distinguishedName){$path = "LDAP://" + $value.distinguishedName} }
}
$path
} `
{Throw "Path readonly"}
Add-Member -in $object ScriptProperty Parent `
{
$path = $this.Path
If($path -ne $this.Root){$path = ($path -Replace "LDAP://((CN|OU)=.*?,)(OU|CN|DC)=","LDAP://`$3=")}
$path
} `
{Throw "Parent readonly"}
Add-Member -in $object ScriptProperty Name `
{
$name=""
If($this.Path -ne $this.Root){$name = [System.Text.RegularExpressions.Regex]::Match($this.Path ,"LDAP://((CN|OU)=.*?),(OU|CN|DC)=.*").Groups[1].Value}
$name
} `
{
$name = $args[0]
If(!$this.Name){Return}
If (!$name){Return }
If ($name -isnot [string]){throw "must be string"}
$name = $name.Trim()
If (!$name){Return }
If ($name.IndexOf("=") -gt -1){throw "don't input '='"}
$this.Input = ($this.Path -Replace "(LDAP://(CN|OU)=)(.*?)(,(OU|CN|DC)=.*)","`$1$Name`$4")
}
Add-Member -in $object ScriptMethod Add `
{
$cnName = $args[0]
If (!$cnName){Return }
If ($cnName -isnot [string]){throw "must be string"}
$cnName = $cnName.Trim()
If (!$cnName){Return }
If ($cnName -notmatch "(CN|OU)=/w+"){throw "cnName should be 'CN=*' or 'OU=*'"}
$this.Input = ($this.Path -Replace "(LDAP://)(.*)","`$1$cnName,`$2")
}
Add-Member -in $object ScriptMethod GetDirectoryEntry `
{
[adsi]$this.Path
}
$object
}
说明:
可以将New-LdapPath理解为一个类.它实现AD节点路径处理功能。
属性:
Name 获取或重置当前节点路径名称。
Input 获取或设置当前节点路径。
Path 获取以”LDAP://“开头的节点路径,其值由Input决定。只读。
Parent 获取节点路径Path的父节点路径。只读。
Root 获取当前所在域的根节点路径。只读。
方法:
Add 在当前节点路径对应容器中定位子节点路径。
GetDirectoryEntry 返回当前节点路径的DirectoryEntry。
示例:
$Path = New-LdapPath
$Path
结果:
Root : LDAP://DC=mydomain,DC=local
Input :
Path : LDAP://DC=mydomain,DC=local
Parent : LDAP://DC=mydomain,DC=local
Name :
接着:
$path.Input = "CN=Users,DC=mydomain,DC=local"
$Path
结果:
Root : LDAP://DC=mydomain,DC=local
Input : CN=Users,DC=mydomain,DC=local
Path : LDAP://CN=Users,DC=mydomain,DC=local
Parent : LDAP://DC=mydomain,DC=local
Name : CN=Users
接着:
$Path.Add("CN=LzmTW")
$Path
结果:
Root : LDAP://DC=mydomain,DC=local
Input : LDAP://CN=LzmTW,CN=Users,DC=mydomain,DC=local
Path : LDAP://CN=LzmTW,CN=Users,DC=mydomain,DC=local
Parent : LDAP://CN=Users,DC=mydomain,DC=local
Name : CN=LzmTW
接着:
$Path.Name="HanMo"
$Path
结果:
Root : LDAP://DC=mydomain,DC=local
Input : LDAP://CN=HanMo,CN=Users,DC=mydomain,DC=local
Path : LDAP://CN=HanMo,CN=Users,DC=mydomain,DC=local
Parent : LDAP://CN=Users,DC=mydomain,DC=local
Name : CN=HanMo