none
CSV check ad for existing user, fill that user with the phone number RRS feed

  • Question

  • So I've been searching for this for a while but I'm coming up short. What I'm trying to do is take data from one of our databases and export it to a csv/excel file. Then what I want to do is parse through that csv file each user at a time and communicating with AD to see if thats a real user in our domain. If it is I want it to pull the phone number for that user and put it in the telephoneNumber field in AD for that user.

    Closest thing I have is this script:

    add-PSSnapin quest.activeroles.admanagement -erroraction silentlyContinue
    $namea = Read-Host 'What is the file location containing the users, starting with the drive?'
    $nameb = Read-Host 'What is the group you wish to add these users to?'
    -ErrorAction Continue
    Get-Content “$namea” | Add-QADGroupMember “$nameb” -ErrorAction Continue

    I'm fine with typing the path to my file but after that I just want it to be automated, also if it finds a user that it can't find to keep note of them by printing those names out. I know most of the time you guys always want to see what I've tried for myself. So here it goes:

    add-PSSnapin quest.activeroles.admanagement -erroraction silentlyContinue
    $namea = Read-Host 'What is the file location containing the users, starting with the drive?'
    Get-Content "$namea" | Add telephonenumber field

    But I don't know how for it to detect the telephone number also in the get-content and seperate that from the actual persons name.

    Any help is appreciated. Also my -erroraction silentlyContinue doesn't work for some reason?

    Thanks!!!

    Friday, January 17, 2014 2:13 PM

Answers

  • Hi,

    I'm going to make some assumptions here, because I'm not really clear on your requirements.

    First, you have a CSV file named userList.csv with two columns - UserName and OfficePhone.

    Second, you don't want to overwrite any existing phone numbers.

    Third, you have access to the AD cmdlets.

    If you can meet all of those, this will do what you're after:

    Import-Csv .\userList.csv | ForEach {
    
        $ErrorActionPreference = 'SilentlyContinue'
        $user = Get-ADUser $_.UserName -Properties OfficePhone
        $ErrorActionPreference = 'Continue'
    
        If ($user) {
    
            If (!($user.OfficePhone)) { Set-ADUser -Identity $_.UserName -OfficePhone $_.OfficePhone }
            Remove-Variable user
    
        }
    }


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    • Marked as answer by jjthexer Friday, January 17, 2014 3:30 PM
    Friday, January 17, 2014 2:38 PM

All replies

  • Hi,

    I'm going to make some assumptions here, because I'm not really clear on your requirements.

    First, you have a CSV file named userList.csv with two columns - UserName and OfficePhone.

    Second, you don't want to overwrite any existing phone numbers.

    Third, you have access to the AD cmdlets.

    If you can meet all of those, this will do what you're after:

    Import-Csv .\userList.csv | ForEach {
    
        $ErrorActionPreference = 'SilentlyContinue'
        $user = Get-ADUser $_.UserName -Properties OfficePhone
        $ErrorActionPreference = 'Continue'
    
        If ($user) {
    
            If (!($user.OfficePhone)) { Set-ADUser -Identity $_.UserName -OfficePhone $_.OfficePhone }
            Remove-Variable user
    
        }
    }


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    • Marked as answer by jjthexer Friday, January 17, 2014 3:30 PM
    Friday, January 17, 2014 2:38 PM
  • Hi jjthexer,

    here, I'll throw in a frame you can use by adding the meat of the script into it:

    # The path where you store your csv file that includes the user-information
    $path = "C:\ExampleFolder\example.csv"
    
    # Load the Csv file into PowerShell
    $users = Import-Csv $path
    
    # Now do for each of those
    foreach ($User in $users)
    {
    	# Step 1: Check whether the user exists
    	
    	# Step 2: If it exists, add telephone number to AD object
    }

    You'll need to figure out how to fill out the foreach loop yourself (there are plenty of learning ressources here on technet to do so on your own), or wait until someone else cheats and tells you more ;).

    Noticed that I changed the "Get-Content" into "Import-Csv"? It alters the way you receive the data. Suffice to say, Import-Csv is cooler when dealing with Csv files. Try out these two commands (after declaring the path variable to your csv file) to see the difference:

    (Get-Content $path)[1] | FL *
    
    (Import-Csv $path)[0] | FL *

    Another thing you used is the add-pssnapin command. What this does is load the Quest Commands to access the Active Directory. For it to work, you need to have these installed. There is a Microsoft native command set that is available on all computers that have an AD Management Console. Either of those will work, if you meet the stated prerequisites. Both are best placed at the head of the script. Here are the two commands again for reference:

    # Load Quest AD Cmdlets
    Add-PSSnapin quest.activeroles.admanagement
    
    # Load Microsoft AD Cmdlets
    Import-Module ActiveDirectory

    Hopefully, that info will be of some help to you :)

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Friday, January 17, 2014 2:39 PM
  • I tried this but the command ended quick and did not work. Also it didn't throw any errors.
    Friday, January 17, 2014 3:19 PM
  • Thank you, I like your formatting it outlines basically what you need to do. Thank you for the tips I will keep researching.
    Friday, January 17, 2014 3:20 PM
  • I tried this but the command ended quick and did not work. Also it didn't throw any errors.

    I'll assume this was meant for me. Do you actually meet the requirements I laid out?

    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    Friday, January 17, 2014 3:29 PM
  • I do, & actually I had to look up the import cmdlet and figured out just because it says import-csv you don't have to use a csv file, changed it to .txt & BOOM worked.

    Thank you so much :) I'm marking yours as the answer & voting you up! <3

    Friday, January 17, 2014 3:30 PM
  • Cheers, you're very welcome. Glad I could help out.

    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    Friday, January 17, 2014 3:41 PM