Automate Active Directory Integration Tasks from PowerShell
The CData ADO.NET Provider for Active Directory is a standard ADO.NET Provider that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Active Directory.
ADO.NET Provider
The ADO.NET Provider provides a SQL interface for Active Directory; this tutorial shows how to use the Provider to create, retrieve, update, and delete Active Directory data.
Once you have acquired the necessary connection properties, accessing Active Directory data in PowerShell can be enabled in three steps.
To establish a connection, set the following properties:
- Valid User and Password credentials (e.g., Domain\BobF or cn=Bob F,ou=Employees,dc=Domain).
- Server information, including the IP or host name of the Server, as well as the Port.
BaseDN: This will limit the scope of LDAP searches to the height of the distinguished name provided.
Note: Specifying a narrow BaseDN may greatly increase performance; for example, cn=users,dc=domain will only return results contained within cn=users and its children.
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Active Directory\lib\System.Data.CData.ActiveDirectory.dll") -
Connect to Active Directory:
$conn= New-Object System.Data.CData.ActiveDirectory.ActiveDirectoryConnection("User=cn=Bob F,ou=Employees,dc=Domain;Password=bob123;Server=10.0.1.2;Port=389;") $conn.Open() -
Instantiate the ActiveDirectoryDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Id, LogonCount from User" $da= New-Object System.Data.CData.ActiveDirectory.ActiveDirectoryDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.id $_.logoncount }
Update Active Directory Data
$cmd = New-Object System.Data.CData.ActiveDirectory.ActiveDirectoryCommand("UPDATE User SET CN='Administrator' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.ActiveDirectory.ActiveDirectoryParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert Active Directory Data
$cmd = New-Object System.Data.CData.ActiveDirectory.ActiveDirectoryCommand("INSERT INTO User (CN) VALUES (@myCN)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.ActiveDirectory.ActiveDirectoryParameter("@myCN","Administrator")))
$cmd.ExecuteNonQuery()
Delete Active Directory Data
$cmd = New-Object System.Data.CData.ActiveDirectory.ActiveDirectoryCommand("DELETE FROM User WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.ActiveDirectory.ActiveDirectoryParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject