PowerShell與AD之節點路徑處理:New-LdapPath

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章