CSV input to Active Directory

I'm working on a script to update AD based on a CSV from Human Resources. It's working for the most part but I need help with 2 things.

Is it possible to get a summary of the missing/not found accounts at the end of the script? And then I'm confused on how I can make the -manager not change if the account is not found?



$userbase = import-csv \\caph-fs2\home\jacobd\desktop\userbase.csv

foreach ($name in $userbase)

  {
    $First  = $Name.FirstName
    $last   = $Name.LastName
       $user=Get-ADUser -Filter "GivenName -eq '$First' -and Surname -like '$last'" 
        if( $User )
     {

        Write-host $name.firstname $name.lastname $name.department $name.division $name.title
       ##  $User | set-aduser -department $name.department -division $name.division -title $name.title -reports {get-aduser -filter {CN -like "$name.manager"}}
     }
     else
     {
     New-Object PSObject -Property @{
             GivenName      = $First
             Surname        = $last
             SamAccountName = 'MISSING ACCOUNT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
             }
     }
 
 }

Open in new window

LVL 7
Jacob DurhamIT Support Analyst II (Lead Infrastructure Engineer)Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dustin SaundersDirector of OperationsCommented:
You can create an array and store the missing users.  Generally, when making AD changes, good to log the changes out somewhere just in case.

$userbase = import-csv \\caph-fs2\home\jacobd\desktop\userbase.csv
$missingAccounts = @()
$logpath = "C:\path\log.txt" #change this

function WriteLog($message)
{
    Add-Content -Path $logpath -Value "$((Get-Date).ToShortTimeString()) :: $message"
}

foreach($name in $userbase)
{
    $user = Get-ADUser -Filter "GivenName -eq '$($name.FirstName)' -and SN -eq '$($name.LastName)'"
    if ($user)
    {
        WriteLog "Found user $($name.FirstName) $($name.LastName)""
        Write-Host $name.FirstName $name.LastName $name.Department $name.Division $name.Title
    }
    else
    {
        WriteLog "Could Not Find user $($name.SAMAccountName)"
        $missingAccounts += $name
    }
}

Write-Host "The following accounts were not found"
$missingAccounts | %{ Write-Host "Could not find $($_.FirstName) $($_.LastName)" -ForegroundColor Red }
$missingAccounts | %{ WriteLog "Could not find $($_.FirstName) $($_.LastName)" }

Open in new window


I'm not sure I understand what you are asking in the second part of the request.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.