Rammy Charles
asked on
PowerShell Script for Importing Data from CSV into Active Directory
We are looking for a script that will take data from a .csv file (username, Telephone Number, Mobile Number, IP Phone) and import it into a users AD profile accordingly.
Appreciate any assistants.
Appreciate any assistants.
ASKER
I am receiving the below error:
Set-User : The term 'Set-User' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:61
+ foreach($user in import-csv C:\Temp\ADCiscoUCSTestLoad .csv){Set- User $user.usern ...
+ ~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Set-User:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Set-User : The term 'Set-User' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:61
+ foreach($user in import-csv C:\Temp\ADCiscoUCSTestLoad
+ ~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Set-User:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
ASKER
Thanks. I tried that. This is what I have:
Import-Module ActiveDirectory
foreach($user in import-csv C:\Temp\ADCiscoUCSTestLoad .csv){Set- User $user.username -Phone $user.telephone -MobilePhone $user.mobile -OtherTelephone $user.IPphone}
Import-Module ActiveDirectory
foreach($user in import-csv C:\Temp\ADCiscoUCSTestLoad
Try below command. The command that I gave earlier works on Exchange only.
Import-Module ActiveDirectory
foreach($user in import-csv filepath){
Set-ADUser $user.username -OfficePhone $user.telephone -MobilePhone $user.mobile
$IPPhone = (GET-ADUSER $user.username -PROPERTIES ipphone).ipphone
Set-ADUser $user.username -Add @{ipPhone=$ipphone}
}
ASKER
Still no go (see below).
Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command
again.
At line:3 char:12
+ Set-ADUser $user.username -OfficePhone $user.telephone -MobilePhone $user.mobile
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidation Exception
+ FullyQualifiedErrorId : ParameterArgumentValidatio nError,Mic rosoft.Act iveDirecto ry.Managem ent.Comman ds.SetADUs er
Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null or an element of the argument collection contains a null value.
At line:4 char:24
+ $IPPhone = (GET-ADUSER $user.username -PROPERTIES ipphone).ipphone
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidation Exception
+ FullyQualifiedErrorId : ParameterArgumentValidatio nError,Mic rosoft.Act iveDirecto ry.Managem ent.Comman ds.GetADUs er
Set-ADUser : Cannot validate argument on parameter 'Add'. The argument is null or an element of the argument collection contains a null value.
At line:5 char:32
+ Set-ADUser $user.username -Add @{ipPhone=$ipphone}
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidation Exception
Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command
again.
At line:3 char:12
+ Set-ADUser $user.username -OfficePhone $user.telephone -MobilePhone $user.mobile
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidation
+ FullyQualifiedErrorId : ParameterArgumentValidatio
Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null or an element of the argument collection contains a null value.
At line:4 char:24
+ $IPPhone = (GET-ADUSER $user.username -PROPERTIES ipphone).ipphone
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidation
+ FullyQualifiedErrorId : ParameterArgumentValidatio
Set-ADUser : Cannot validate argument on parameter 'Add'. The argument is null or an element of the argument collection contains a null value.
At line:5 char:32
+ Set-ADUser $user.username -Add @{ipPhone=$ipphone}
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidation
ASKER
Below is the name of the columns we have in the csv file.
User Telephone number Mobile number IP Phone
User Telephone number Mobile number IP Phone
Remove space between the above headings as below and run the following script
User Telephonenumber Mobilenumber IPPhone
User Telephonenumber Mobilenumber IPPhone
Import-Module ActiveDirectory
foreach($user in import-csv filepath){
Set-ADUser $user.user -OfficePhone $user.telephonenumber -MobilePhone $user.mobilenumber
$Phone = GET-ADUSER $user.user -PROPERTIES ipphone
$Phone.ipphone = $user.ipphone
set-aduser -Instance $phone}
}
ASKER
Works great. Thanks.
How should that work? Import-CSV expects comma as delimiter by default. Neither tab nor space are valid unless explicitely stated.
Setting the user can be simplified:
Setting the user can be simplified:
Import-Module ActiveDirectory
foreach ($user in import-csv filepath -Delimiter "`t"){
Set-ADUser $user.user -Replace @{
OfficePhone = $user.telephonenumber
MobilePhone = $user.mobilenumber
IPPhone = $user.ipphone
}
}
ASKER
One more question, the script appears to fail if any of the cells are not populated. Is there any way to execute the script even if one or more of the fields is not present in the csv (i.e. ip phone for one or more users)?
ASKER
QLemo I received the below error with your script.
Set-ADUser : Cannot validate argument on parameter 'Replace'. The argument is null or an element of the argument collection contains a null value.
At line:3 char:34
+ Set-ADUser $user.user -Replace @{
+ ~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidation Exception
+ FullyQualifiedErrorId : ParameterArgumentValidatio nError,Mic rosoft.Act iveDirecto ry.Managem ent.Comman ds.SetADUs er
Set-ADUser : Cannot validate argument on parameter 'Replace'. The argument is null or an element of the argument collection contains a null value.
At line:3 char:34
+ Set-ADUser $user.user -Replace @{
+ ~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidation Exception
+ FullyQualifiedErrorId : ParameterArgumentValidatio nError,Mic rosoft.Act iveDirecto ry.Managem ent.Comman ds.SetADUs er
Set-ADUser : Cannot validate argument on parameter 'Replace'. The argument is null or an element of the argument collection contains a null value.
At line:3 char:34
+ Set-ADUser $user.user -Replace @{
+ ~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidation Exception
+ FullyQualifiedErrorId : ParameterArgumentValidatio nError,Mic rosoft.Act iveDirecto ry.Managem ent.Comman ds.SetADUs er
Set-ADUser : Cannot validate argument on parameter 'Replace'. The argument is null or an element of the argument collection contains a null value.
At line:3 char:34
+ Set-ADUser $user.user -Replace @{
+ ~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidation Exception
+ FullyQualifiedErrorId : ParameterArgumentValidatio nError,Mic rosoft.Act iveDirecto ry.Managem ent.Comman ds.SetADUs er
Set-ADUser : Cannot validate argument on parameter 'Replace'. The argument is null or an element of the argument collection contains a null value.
At line:3 char:34
+ Set-ADUser $user.user -Replace @{
+ ~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidation Exception
+ FullyQualifiedErrorId : ParameterArgumentValidatio nError,Mic rosoft.Act iveDirecto ry.Managem ent.Comman ds.SetADUs er
Set-ADUser : Cannot validate argument on parameter 'Replace'. The argument is null or an element of the argument collection contains a null value.
At line:3 char:34
+ Set-ADUser $user.user -Replace @{
+ ~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidation
+ FullyQualifiedErrorId : ParameterArgumentValidatio
Set-ADUser : Cannot validate argument on parameter 'Replace'. The argument is null or an element of the argument collection contains a null value.
At line:3 char:34
+ Set-ADUser $user.user -Replace @{
+ ~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidation
+ FullyQualifiedErrorId : ParameterArgumentValidatio
Set-ADUser : Cannot validate argument on parameter 'Replace'. The argument is null or an element of the argument collection contains a null value.
At line:3 char:34
+ Set-ADUser $user.user -Replace @{
+ ~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidation
+ FullyQualifiedErrorId : ParameterArgumentValidatio
Set-ADUser : Cannot validate argument on parameter 'Replace'. The argument is null or an element of the argument collection contains a null value.
At line:3 char:34
+ Set-ADUser $user.user -Replace @{
+ ~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidation
+ FullyQualifiedErrorId : ParameterArgumentValidatio
Set-ADUser : Cannot validate argument on parameter 'Replace'. The argument is null or an element of the argument collection contains a null value.
At line:3 char:34
+ Set-ADUser $user.user -Replace @{
+ ~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidation
+ FullyQualifiedErrorId : ParameterArgumentValidatio
You may try this.
@Qlemo - It worked because the default delimiter in csv is a comma.
Import-Module ActiveDirectory
foreach($user in import-csv filepath){
Set-ADUser $user.user -OfficePhone $user.telephonenumber -MobilePhone $user.mobilenumber
if($user.ipphone -ne ""){$Phone = GET-ADUSER $user.user -PROPERTIES ipphone
$Phone.ipphone = $user.ipphone
set-aduser -Instance $phone}
}
@Qlemo - It worked because the default delimiter in csv is a comma.
ASKER
Thanks. Received the below error.
Set-ADUser : replace
At line:3 char:1
+ Set-ADUser $user.user -OfficePhone $user.telephonenumber -MobilePhone $user.mobi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
+ CategoryInfo : InvalidOperation: (BurgessV:ADUser) [Set-ADUser], ADInvalidOperationExceptio n
+ FullyQualifiedErrorId : ActiveDirectoryServer:0,Mi crosoft.Ac tiveDirect ory.Manage ment.Comma nds.SetADU ser
Set-ADUser : replace
At line:3 char:1
+ Set-ADUser $user.user -OfficePhone $user.telephonenumber -MobilePhone $user.mobi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (BurgessV:ADUser) [Set-ADUser], ADInvalidOperationExceptio
+ FullyQualifiedErrorId : ActiveDirectoryServer:0,Mi
Please provide the complete error. the text that you posted does not include the reason for error
ASKER
That is the full error message. It looks like it is failing because that user does not have all cells populated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I've requested that this question be closed as follows:
Accepted answer: 0 points for Narvaezj's comment #a40489233
for the following reason:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
Accepted answer: 0 points for Narvaezj's comment #a40489233
for the following reason:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
This command will update the phone numbers for each user mentioned under username column. As you have not specified the AD properties I assumed the following and gave the command. You can update the properties accordingly as per your requirement.
Open in new window