Solved

PowerShell Script for Importing Data from CSV into Active Directory

Posted on 2014-12-09
21
736 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
  • 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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 69

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 250 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 69

Assisted Solution

by:Qlemo
Qlemo earned 250 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 46

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 69

Expert Comment

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

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This article runs through the process of deploying a single EXE application selectively to a group of user.
This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

839 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