Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PowerShell Script for Importing Data from CSV into Active Directory

Posted on 2014-12-09
21
Medium Priority
?
962 Views
Last Modified: 2015-01-02
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.
0
Comment
Question by:GR JN
[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
  • 9
  • 6
  • 3
  • +1
21 Comments
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 40489023
Your input file should have the first column as username,telephone,mobile,ipphone.

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.

foreach($user in import-csv filepath){Set-User $user.username -Phone $user.telephone -MobilePhone $user.mobile -OtherTelephone $user.IPphone}

Open in new window

0
 

Author Comment

by:GR JN
ID: 40489073
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
0
 

Author Comment

by:GR JN
ID: 40489085
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}
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 40489111
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}
}

Open in new window

0
 

Author Comment

by:GR JN
ID: 40489121
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], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser
 
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], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser
 
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], ParameterBindingValidationException
0
 

Author Comment

by:GR JN
ID: 40489123
Below is the name of the columns we have in the csv file.

User      Telephone number      Mobile number      IP Phone
0
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 40489147
Remove space between the above headings as below and run the following script

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}
}

Open in new window

0
 

Author Comment

by:GR JN
ID: 40489233
Works great. Thanks.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 40489306
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:
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
  }
}

Open in new window

0
 

Author Comment

by:GR JN
ID: 40489502
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)?
0
 

Author Comment

by:GR JN
ID: 40489725
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], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser
 
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], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser
 
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], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser
 
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], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser
 
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], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser
0
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 40489726
You may try this.

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}
}

Open in new window



@Qlemo - It worked because the default delimiter in csv is a comma.
0
 

Author Comment

by:GR JN
ID: 40489732
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], ADInvalidOperationException
    + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser
0
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 40489747
Please provide the complete error. the text that you posted does not include the reason for error
0
 

Author Comment

by:GR JN
ID: 40489778
That is the full error message. It looks like it is failing because that user does not have all cells populated.
0
 
LVL 16

Accepted Solution

by:
Rajitha Chimmani earned 1000 total points
ID: 40489886
Ideally, the script should continue to execute even if the cell value is empty just that the ip phone for that user will not be updated. Please try this

Import-Module ActiveDirectory
foreach($user in import-csv filepath){
Set-ADUser $user.user -OfficePhone $user.telephonenumber -MobilePhone $user.mobilenumber
if($user.ipphone -eq ""){$Phone = GET-ADUSER $user.user -PROPERTIES ipphone
set-aduser -Instance $phone}
if($user.ipphone -ne ""){$Phone = GET-ADUSER $user.user -PROPERTIES ipphone
$Phone.ipphone = $user.ipphone
set-aduser -Instance $phone}
}

Open in new window

0
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 1000 total points
ID: 40490194
Yes, empty properties are an issue. IMHO it is best to work with dynamic replace hashtables, only filled with data if something has been found in the CSV:
Import-Module ActiveDirectory
Import-Csv 'C:\Temp\EE\Users.CSV') | % {
  $replace = @{}
  if ($_.TelephoneNumber) { $replace.Add('telephoneNumber', $_.TelephoneNumber) }  <# -OfficePhone #>
  if ($_.MobileNumber   ) { $replace.Add('mobile'         , $_.MobileNumber   ) }  <# -MobilePhone #>
  if ($_.IPPhone        ) { $replace.Add('ipphone'        , $_.IPPhone        ) }
  Set-ADUser -identity $_.User -Replace @replace
}

Open in new window

This assumes, despite what was has been posted tells different, that the data is comma-separated.
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 40526879
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.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 40526880
Objection. Correct answers are
  http:#a40489886
  http:#a40490194
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Group policies can be applied selectively to specific devices with the help of groups. Utilising this, it is possible to phase-in group policies, over a period of time, by randomly adding non-members user or computers at a set interval, to a group f…
This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

604 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