how do I fix my script

Marlon Womack 0 Reputation points
2024-10-04T19:09:00.23+00:00

$Users = Import-Csv C:\Tmp\TestAccount.csv -Delimiter ';'

foreach ($user in $Users) {

# properties that are part of Set-ADUser

$Hashtable = @{

    OfficePhone = $user.telephoneNumber

}

Set-ADUser -Identity $user.sAMAccountName @Hashtable

}

Windows for business | Windows Server | User experience | PowerShell
{count} votes

2 answers

Sort by: Most helpful
  1. Q&A Assist
    2024-10-04T19:09:00.2333333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Your script looks mostly correct, but there are a few things to check to ensure it runs smoothly:

    1. Check the CSV Format: Ensure that the CSV file C:\Tmp\TestAccount.csv is correctly formatted and that the delimiter used is indeed a semicolon (;). If the file uses commas, you should change -Delimiter ';' to -Delimiter ','.
    2. Property Names: Make sure that the property names in the CSV (like telephoneNumber and sAMAccountName) match exactly with the headers in your CSV file. PowerShell is case-sensitive when accessing properties.
    3. Error Handling: Consider adding error handling to catch any issues that may arise when calling Set-ADUser. You can use Try-Catch blocks for this.

    Here’s a slightly modified version of your script with error handling:

    $Users = Import-Csv C:\Tmp\TestAccount.csv -Delimiter ';'
    
    foreach ($user in $Users) {
        $Hashtable = @{
            OfficePhone = $user.telephoneNumber
        }
    
        try {
            Set-ADUser -Identity $user.sAMAccountName @Hashtable
        } catch {
            Write-Host "Error updating user $($user.sAMAccountName): $_"
        }
    }
    

    This will help you identify any issues that occur during the execution of Set-ADUser.


    References:

    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2024-10-04T22:59:00.16+00:00

    If the error is "Cannot validate argument on parameter 'Identity'" then the CSV doesn't have a column named "sAMAccountName" (as you script is expecting), or there's a blank line in the CSV, or the CSV delimiter isn't the ';' character.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.