0

I am writing a code to update an Active Directory using input from a CSV file. I am able to get the script to run all of the way to the end, but when I check the Active Directory, it is not updated. When I check the log file, it does not list any CSV file as being used. Everything is in the same folder and the log file writes fine.

I have tried altering the path that the script searches for the CSV file to C:/PATH/TO/FILE/filename I have also tried with quotes, without quotes, with -Path, and without -Path. I have looked at the other StackOverflow articles and they all seem to say that this should work... I think the key line here is the 3rd from the top. "$Users = Import-csv -Path users.csv" I could be wrong though.


        Import-Module ActiveDirectory
        #Import and Specify CSV File
        $Users = Import-csv -Path users.csv
        #Specify Log File
        $LogFile = ".\UpdateUsersCsv.log"

        # Write to the log file. Abort the script if the log file cannot be                 updated.
        Add-Content -Path $LogFile -Value                                 "================================================"
        Add-Content -Path $LogFile -Value "UpdateUsersCsv.ps1"
        Add-Content -Path $LogFile -Value "Update Users from CSV file: $Users"
        Add-Content -Path $LogFile -Value "Started: $((Get-Date).ToString())"
        Add-Content -Path $LogFile -Value "Log file: $LogFile"
        Add-Content -Path $LogFile -Value "------------------------------------------------"

        # Initialize counters.
        $Updated = 0
        $NotFound = 0

        #Find Users
        foreach($User in $Users){
            $ADUser = Get-ADUser
            #If found, update Users and increment $Updated
            If($ADUser){
                Set-ADUser $User.SamAccountName -Description $User.NewDescription
                $Updated = $Updated + 1
            #If not found, increment $NotFound
            }ElseIf(!$ADUser){
                Add-Content -Path $LogFile -Value "User $ASUser not found."
                       $NotFound = NotFound + 1
            }
        }


        Add-Content -Path $LogFile -Value "Number of users updated: $Updated"
        Add-Content -Path $LogFile -Value "Number of users not found: $NotFound"

        Add-Content -Path $LogFile -Value         "================================================"

        Write-Host "Script Completed, check log file $LogFile for more information."

The Active directory should be updated with whatever information is in the CSV file. For example, if I used : myName, myDescription goes here It should update the description of the user called "myName" to say "myDescription goes here" Then the log file should say something along the lines of: From csv file: nameOfCSV Users updated = 1 Users not found = 0

If the user is not found, it should report that accordingly, but that is not my current problem.

When I run the script and open the log file, there is no csv file shown, and the users updated and users not found fields are left at 0. This leads me to believe that the problem is that the csv file is not importing.

10
  • after you run the $Users line, is anything in that $Var? Commented Jul 29, 2019 at 19:01
  • 1
    I see one issue... ElseIf(!ADUser) should be ElseIf(!$ADUser) Commented Jul 29, 2019 at 19:05
  • 1
    Also, line 9 will not report the path to the file, that's not the value of $Users. $Users is an array of objects representing the users you want added/updated. Commented Jul 29, 2019 at 19:06
  • 1
    Since you are hard coding the path you would hard code writing it to the file. What I would suggest is to do something like: Add-Content -Path $LogFile -Value "Update $($Users.count) Users from CSV file: Users.csv" That way if it says 1 or more users you know it loaded the CSV. Otherwise set a variable like $CSVPath = Resolve-Path 'users.csv' and then use that when importing it and when logging it. Commented Jul 29, 2019 at 19:16
  • 1
    Shouldn't this $ADUser = Get-ADUser be $ADUser = Get-ADUser $user.<HeaderUsedInTheCSVforTheUserColumn> Commented Jul 29, 2019 at 19:25

1 Answer 1

2

I see what the problem is. Your code will only update the AD, if the user is found. The command to look for an AD user is Get-Aduser -identity <SamAccountName>

In your code, you have simply used $ADUser = Get-ADUser without any parameters. I am surprised why you do not get any errors or prompt that asks for -identity parameter value.

In your csv, you probably have a column like this.

enter image description here

Then in your foreach loop would change like

foreach($User in $Users){
    $ADUser = Get-ADUser $User.UserID
...
}

Now, if the user exists, your code will execute the Set-AdUser block. In your current code, since it can't actually find the user, $Aduser would presumably (still surprised it gets that far without prompting for Identity) be empty and would always hit the ElseIf block.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked great! Thanks for all the help. I have one more question, but I am just curious about it, it is not a problem. Does the CSV have to be in the exact format that you show? Or can it be any format as long as the code can follow it? Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.