gm13
gm13

Reputation: 1

Attempting to import office phone numbers into AD using sAMAccountName

I'm getting an error on this script that I just can't figure out. I would greatly appreciate any assistance! I'm pretty new to scripting, so this is a little cobbled together from other code I found.

I'm just trying to make a script that will use the SAMAccountName to locate the object and add the corresponding office phone number from the CSV file.

Code:

    #Import AD Module
Import-Module ActiveDirectory

#Import the CSV and set a delimiter
$UserList = Import-Csv -Delimiter ';' -Path C:\PhoneNumber.csv

#Foreach against the CSV
ForEach($User in $UserList){
    #Assign Variable for the user itself (using the SamAccountName and OfficePhone column)
    $User = $U.SamAccountName
    $OfficePhone = $U.OfficeNumber
    #Now assign a office number to the user you just define (Fixes the identity issue)
    Get-ADUser -Identity $user.SamAccountName | Set-ADuser -OfficePhone $OfficePhone
}

Error:

Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null or an element of the argument collection 
contains a null value.
At C:\PhoneNumberImport.ps1:10 char:26
+     Get-ADUser -Identity $User.SamAccountName | Set-ADuser -OfficePho ...
+                          ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser

What am I doing wrong?

Upvotes: 0

Views: 427

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 60518

Nevermind my last answer, the error should be on this line:

$User = $U.SamAccountName

Note that, the variable $U is not defined anywhere hence when you do $User = $U.SamAccountName you're setting the variable to $null which is why Get-ADUser throws that error.

Try this code below:

ForEach($User in $UserList){
    Get-ADUser -Identity $user.SamAccountName |
    Set-ADuser -OfficePhone $user.OfficeNumber
}

Upvotes: 1

Related Questions