Solved

PowerShell Script for Importing Data from CSV into Active Directory

Posted on 2014-12-09
21
663 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 16

Expert Comment

by:Rajitha Chimmani
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Works great. Thanks.
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:GR JN
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Please provide the complete error. the text that you posted does not include the reason for error
0
 

Author Comment

by:GR JN
Comment Utility
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
Comment Utility
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 68

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
Comment Utility
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 45

Expert Comment

by:Martin Liss
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
Objection. Correct answers are
  http:#a40489886
  http:#a40490194
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now