Solved

Update attribute in AD with data in SQL table via batch or script

Posted on 2009-07-14
1
312 Views
Last Modified: 2013-12-24
I have two fields in an SQL 2K5 table (tblOne) named Name and ExtID. I need to run a loop and match each value in the Name field to saMAccountname fields in AD and update extensionAttribute12 with the correlating value in ExtID. Script or Batch is fine
0
Comment
Question by:Mike Miller
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 71

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 24859235

How about a bit of PowerShell?

It uses these two:

http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx
http://www.quest.com/activeroles-server/arms.aspx

Both are free. You'd have to run PowerShell using the shortcut under the quest forlder. Or include this line at the beginning of the script:

Add-PsSnapIn "Quest.ActiveRoles.ADManagement"

The code below can run directly from the PowerShell prompt. If scheduling this then it should be saved as a .ps1 file and can be called with "PowerShell.exe ScriptName.ps1".

Chris
$Server = "TheSQLServer"
$Database = "TheDatabaseName"
$SqlQuery = "SELECT * FROM tblOne"
 
# Setup SQL Connection
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$Server;Database =$Database;Integrated Security=True"
 
# Setup SQL Command
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
 
# Setup .NET SQLAdapter to execute and fill .NET Dataset
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
 
#Execute and Get Row Count
$nRecs = $SqlAdapter.Fill($DataSet)
 
if ($nRecs -gt 0)
{
  $DataSet.Tables[0] | %{
 
    # This line gets the user by sAMAccountName and sets extensionAttribute 12 for that user.
    # Note that -WhatIf is used here for testing. Remove once happy.
 
    Get-QADUser $_.Name | Set-QADUser -ObjectAttributes @{extensionAttribute12='$($_.ExtID)'} -WhatIf
  }
}

Open in new window

0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question