简体   繁体   中英

How do I get my script to use full names to find AD account?

I am very new to powershell, still trying to figure out how it works. I have so far written a short script to take details from a CSV and poulate properties in AD. If I use the username ie smithj it works fine but I can't get it to take a name like John Smith and find the account it is associated with. This is the same with the manager field, it will take the username but I cant get it to take a full name.

Any help or advice would be much appreciated.

Import-module ActiveDirectory  
    $List = Import-CSV "\\SharedServer\shared\MYCSV.csv" | % { 
        $User = $_.UserName
        $ID = $_.EmployeeID 
        $EmployeeNumber = $_.EmployeeNumber
        $Description = $_.Description
        $Department = $_.Department
        $Title = $_.Title
        $AccountExpirationDate = $_.AccountExpire
        $Manager = $_.Manager

Set-ADUser $User -employeeID $ID -EmployeeNumber $EmployeeNumber -Description $Description -Department $Department -Title $Title -Manager $Manager -AccountExpirationDate $AccountExpirationDate 
}

I would use Get-ADUser and then pipe the object that was returned into Set-ADUser . Here is a quick example:

Get-ADUser -Filter " Name -eq 'Name here' " | Set-ADUser -employeeID $ID 

Depending on what the CSV contains for UserName and Manager , the best would be to have the SamAccountName or DistinguishedName because these attributes are unique within the same domain. UserPrincipalName or EmailAddress would also do nicely for targeting the correct user.

From your question however, I gather that the CSV has the users Name in there that should correspond to the Name property of an AD user.

In that case I agree with IT Delinquent that you can use that in the Filter parameter for Get-ADUser and that is also what my example code below uses.

Then there is the question of how you have entered the date for the AccountExpirationDate in the CSV file..
This parameter wants a DateTime object , not a string, so you'll have to convert that before use.

Finally, I would suggest using Splatting for cmdlets like Set-ADUser that take a lot of parameters.

Something like this:

Import-CSV "\\SharedServer\shared\MYCSV.csv" | ForEach-Object { 
    $user = Get-ADUser -Filter "Name -eq '$($_.UserName)'" -ErrorAction SilentlyContinue
    if (!$user) {
        Write-Warning "User '$($_.UserName)' not found"
    }
    else {
        # convert the date string from the CSV into a real DateTime object
        # Since I cannot see the CSV, you may need to do this using [DateTime]::ParseExact()
        $expireDate = Get-Date $_.AccountExpire

        # create a Hashtable for the parameters
        $userProps = @{
            'EmployeeID'            = $_.EmployeeID
            'EmployeeNumber'        = $_.EmployeeNumber
            'Description'           = $_.Description
            'Department'            = $_.Department
            'Title'                 = $_.Title
            'AccountExpirationDate' = $expireDate
        }
        # get the manager object from the name
        $manager = Get-ADUser -Filter "Name -eq '$($_.Manager)'" -ErrorAction SilentlyContinue
        if ($manager) {
            $userProps['Manager'] = $manager.DistinguishedName
        }

        $user | Set-ADUser @userProps
    }
}

When using UserPrincipalName or EmailAddress , change the Filter into "UserPrincipalName -eq '$($_.UserName)'" or "EmailAddress -eq '$($_.UserName)'" .
You might even want to experiment with Ambiguous Name Resolution ..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM