LDAP-Erweiterungen für die PowerShell
Autor: Dr. Holger Schwichtenberg
---------------------------------------------------------------------Author: Dr. Holger SchwichtenbergDesc: PowerShell Commandlets for handling LDAP-ObjectsUsage: This file contains a function-based Commandlet. In order to useit, you must dot source the file into your shell e.g.:PH> . c:\PSExtensions\LDAP_Commandlets.ps1Date: 10/05/2006 Version: 2.0 Host: PowerShell Version 1.0 RC2---------------------------------------------------------------------Get single LDAP objectfunction Get-LDAPObject { param([string[]]$LDAPPath) begin { } process { if ($_) { if ($_ -is [string]) { new-object system.directoryservices.directoryEntry($_) } else { throw "Pipeline input must be [string]." } } } end { if ($LDAPPath) { foreach ($Path in $LDAPPath) { new-object system.directoryservices.directoryEntry($Path) } } } } Get content of an LDAP containerfunction Get-LDAPChildren { param([string[]]$LDAPPath) begin { function getContainer([string] $path) { $con = new-object system.directoryservices.directoryEntry($path) $con.PSBase.Children } } process { if ($_) { if ($_ -is [string]) { getContainer($_) } elseif ($_ -is [System.DirectoryServices.DirectoryEntry]) { getContainer($_.PSBase.Path) } else { throw "Pipeline input must be [string] or [System.DirectoryServices.DirectoryEntry]." } } } end { if ($LDAPPath) { foreach ($Path in $LDAPPath) { getContainer($Path) } } } } Remove an object from an LDAP containerfunction Remove-LDAPObject { param([string[]]$LDAPPath) begin { function remove([string] $path) { if ([system.directoryservices.directoryEntry]::Exists($path)) { $obj = new-object system.directoryservices.directoryEntry($path) $obj.PSBase.DeleteTree() $obj } else { throw "Object does not exists!" } } } process { if ($_) { if ($_ -is [string]) { remove($_) } elseif ($_ -is [System.DirectoryServices.DirectoryEntry]) { remove($_.PSBase.Path) } else { throw "Pipeline input must be [string] or [System.DirectoryServices.DirectoryEntry]." } } } end { if ($LDAPPath) { foreach ($Path in $LDAPPath) { remove($Path) } } } } Add an new object to an LDAP containerfunction Add-LDAPObject { param([string]$Container, [string]$Class, [string]$RDN) begin { } process { } end { if ($Container -and $Class -and $RDN) { if ([system.directoryservices.directoryEntry]::Exists($Container)) { Write-Warning "Adding Object $RDN of type $Class to $Container" $obj = new-object system.directoryservices.directoryEntry($Container) $newobj = $obj.PSBase.Children.Add([string]$RDN,[string]$Class) $newobj.PSBase.CommitChanges() } else { throw "Container does not exists!" } } } } Define alias for functionSet-Alias LDP Get-LDAPObject Set-Alias LDC Get-LDAPObject Set-Alias RLDP RemoveLDAPObject Set-Alias ALDP Add-LDAPObject Confirm installation"Function-based commandlets for LDAP successfully installed!"
function Get-LDAPObject { param([string[]]$LDAPPath) begin { } process { if ($_) { if ($_ -is [string]) { new-object system.directoryservices.directoryEntry($_) } else { throw "Pipeline input must be [string]." } } } end { if ($LDAPPath) { foreach ($Path in $LDAPPath) { new-object system.directoryservices.directoryEntry($Path) } } } }
function Get-LDAPChildren { param([string[]]$LDAPPath) begin { function getContainer([string] $path) { $con = new-object system.directoryservices.directoryEntry($path) $con.PSBase.Children } } process { if ($_) { if ($_ -is [string]) { getContainer($_) } elseif ($_ -is [System.DirectoryServices.DirectoryEntry]) { getContainer($_.PSBase.Path) } else { throw "Pipeline input must be [string] or [System.DirectoryServices.DirectoryEntry]." } } } end { if ($LDAPPath) { foreach ($Path in $LDAPPath) { getContainer($Path) } } } }
function Remove-LDAPObject { param([string[]]$LDAPPath) begin { function remove([string] $path) { if ([system.directoryservices.directoryEntry]::Exists($path)) { $obj = new-object system.directoryservices.directoryEntry($path) $obj.PSBase.DeleteTree() $obj } else { throw "Object does not exists!" } } } process { if ($_) { if ($_ -is [string]) { remove($_) } elseif ($_ -is [System.DirectoryServices.DirectoryEntry]) { remove($_.PSBase.Path) } else { throw "Pipeline input must be [string] or [System.DirectoryServices.DirectoryEntry]." } } } end { if ($LDAPPath) { foreach ($Path in $LDAPPath) { remove($Path) } } } }
function Add-LDAPObject { param([string]$Container, [string]$Class, [string]$RDN) begin { } process { } end { if ($Container -and $Class -and $RDN) { if ([system.directoryservices.directoryEntry]::Exists($Container)) {
$obj = new-object system.directoryservices.directoryEntry($Container) $newobj = $obj.PSBase.Children.Add([string]$RDN,[string]$Class) $newobj.PSBase.CommitChanges() } else { throw "Container does not exists!" } } } }
Set-Alias LDP Get-LDAPObject Set-Alias LDC Get-LDAPObject Set-Alias RLDP RemoveLDAPObject Set-Alias ALDP Add-LDAPObject
"Function-based commandlets for LDAP successfully installed!"
Liste aller Codebeispiele Definition '.NET Framework Class Library' PowerShell Community Portal