Importing user phone numbers from internal SQL DB into AD's telephoneNumber field

Need help adding AD field attribute from an internal SQL DB. I have a working script but was tasked to add phone numbers to it. For some reason it's not moving over the internal phone field we use:

#Debugging
param(
	$DebugPreference="SilentlyContinue" #To turn off Debug: $DebugPreference="SilentlyContinue"
)

# set SQL vars
$constr = 'Server=sqlcluster; Database=XXX; Integrated Security=sspi'
$sqlCmd = 'select userid, pos_title, name_first, name_last, pos_department, [b]con_work_voice[/b], con_mailstop, emp_number from crew where active = 1'

# get according AD info as a indexable hashtable
$ADUsers = @{}
get-aduser -filter { enabled -eq $true } -properties title, SamACcountName, GivenName, Surname, Department, telephoneNumber, roomNumber, employeeID |
  % { $ADUsers += @{ $_.SamAccountName = $_ } }

# meat and potatoes
$set = new-object system.data.dataset
(new-object System.Data.SqlClient.SqlDataAdapter ($sqlCmd, $constr)).Fill($set) | out-null

$set.Tables[0] | % { 
	if ($usr = $ADUsers[$_.userid]) {
		if ($usr.Title -ne $_.pos_title -or $usr.Department -ne $_.pos_department -or $usr.roomNumber -ne $_.con_mailstop -or $usr.employeeID -ne $_.emp_number -or [b]$usr.telephoneNumber -ne $_.con_work_voice) [/b]{
			Write-Debug "Setting User $($_.userid) Title, Department, EmployeeID"
			$usr | Set-ADUser -Title $_.Pos_Title -Department $_.pos_department -employeeID $_.emp_number
			Write-Debug "Setting User $($_.userid) Room #"
			$usr | Set-ADUser -Replace @{roomNumber="$($_.con_mailstop)"}
		}
	} else {
		Write-Warning "*** Error: User $($_.userid) not found in Active Directory!"
	}
}

Open in new window

RubinPostaerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
You only added the test whether the  AD number differs from the DB number (line 21), but did nothing to actually write the field back (line 23).
Assuming the rest works, this should do the trick:
#Debugging
param(
	$DebugPreference="SilentlyContinue" #To turn off Debug: $DebugPreference="SilentlyContinue"
)

# set SQL vars
$constr = 'Server=sqlcluster; Database=XXX; Integrated Security=sspi'
$sqlCmd = 'select userid, pos_title, name_first, name_last, pos_department, con_work_voice, con_mailstop, emp_number from crew where active = 1'

# get according AD info as a indexable hashtable
$ADUsers = @{}
get-aduser -filter { enabled -eq $true } -properties title, SamACcountName, GivenName, Surname, Department, telephoneNumber, roomNumber, employeeID |
  % { $ADUsers += @{ $_.SamAccountName = $_ } }

# meat and potatoes
$set = new-object system.data.dataset
(new-object System.Data.SqlClient.SqlDataAdapter ($sqlCmd, $constr)).Fill($set) | out-null

$set.Tables[0] | % { 
	if ($usr = $ADUsers[$_.userid]) {
		if ($usr.Title -ne $_.pos_title -or $usr.Department -ne $_.pos_department -or $usr.roomNumber -ne $_.con_mailstop -or $usr.employeeID -ne $_.emp_number -or $usr.telephoneNumber -ne $_.con_work_voice) {
			Write-Debug "Setting User $($_.userid) Title, Department, EmployeeID"
			$usr | Set-ADUser -Title $_.Pos_Title -Department $_.pos_department -employeeID $_.emp_number -OfficePhone $_.con_work_voice
			Write-Debug "Setting User $($_.userid) Room #"
			$usr | Set-ADUser -Replace @{roomNumber="$($_.con_mailstop)"}
		}
	} else {
		Write-Warning "*** Error: User $($_.userid) not found in Active Directory!"
	}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RubinPostaerAuthor Commented:
Worked like a charm! Thanks!
0
RubinPostaerAuthor Commented:
It looks like some other applications were pulling from the standard Telephone Number Ad field, so it looks like I need to use an alternate attribute (ipPhone) to set. As this is an LDAP and not a standard AD field (correct me if I'm wrong here), I need to set it as I do the roomNumber. I modified the script to add this, but it's giving me an error on the added:

At C:\RPIT\ipphone.ps1:26 char:9
+             Write-Debug "Setting User $($_.userid) ip phone"
+                  ~
Missing '=' operator after key in hash literal.
At C:\RPIT\ipphone.ps1:19 char:20
+ $set.Tables[0] | % {
+                    ~
Missing closing '}' in statement block.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEqualsInHashLiteral

#Debugging
param(
	$DebugPreference="SilentlyContinue" #To turn off Debug: $DebugPreference="SilentlyContinue"
)

# set SQL vars
$constr = 'Server=sqlcluster; Database=c2; Integrated Security=sspi'
$sqlCmd = 'select userid, pos_title, name_first, name_last, pos_department, con_work_voice, con_mailstop, emp_number from crew where active = 1'

# get according AD info as a indexable hashtable
$ADUsers = @{}
get-aduser -filter { enabled -eq $true } -properties title, SamACcountName, GivenName, Surname, Department, ipPhone, roomNumber, employeeID |
  % { $ADUsers += @{ $_.SamAccountName = $_ } }

# meat and potatoes
$set = new-object system.data.dataset
(new-object System.Data.SqlClient.SqlDataAdapter ($sqlCmd, $constr)).Fill($set) | out-null

$set.Tables[0] | % { 
	if ($usr = $ADUsers[$_.userid]) {
		if ($usr.Title -ne $_.pos_title -or $usr.Department -ne $_.pos_department -or $usr.roomNumber -ne $_.con_mailstop -or $usr.employeeID -ne $_.emp_number -or $usr.ipPhone -ne $_.con_work_voice) {
			Write-Debug "Setting User $($_.userid) Title, Department, EmployeeID"
			$usr | Set-ADUser -Title $_.Pos_Title -Department $_.pos_department -employeeID $_.emp_number -ipPhone $_.con_work_voice
			Write-Debug "Setting User $($_.userid) Room #"
			$usr | Set-ADUser -Replace @{roomNumber="$($_.con_mailstop)"
			Write-Debug "Setting User $($_.userid) ip phone"
			$usr | Set-ADUser -Replace @{ipPhone="$($_.con_work_voice)"}
		}
	} else {
		Write-Warning "*** Error: User $($_.userid) not found in Active Directory!"
	}
}

Open in new window

0
oBdACommented:
Have a close look at (the end of) line 25.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.