Solved

Use Powershell to check users exist in AD and update a CSV file

Posted on 2011-03-24
5
1,475 Views
Last Modified: 2012-05-11
I have a CSV file with the following columns
"EMPLOYEE_NUMBER","UNIQUE_ID","FIRST_NAME","LAST_NAME","SAMACCOUNT"

I want to test whether or not the sAMAccount is in AD or not. I want to capture the status in a new column called "STATUS" (Y or N) so my output file should look like this:
"EMPLOYEE_NUMBER","UNIQUE_ID","FIRST_NAME","LAST_NAME","SAMACCOUNT","STATUS"

I'm having trouble reusing a piece of code that someone gave me for a different thing..but thought I could use it for this task.
Function CheckADUser($filename, $output)
{
    $csv = Get-Content -Path $filename

    # This adds the column...but I'm having trouble adding the result of each iteration to the file
    $csv[0] += ",STATUS"
    $b
    foreach($line in $csv)
    {

        $user = Get-QADUser -Name $line.SAMACCOUNT
        if ($user){
		
# I dont know how to add the "Y" under the STATUS column		
$b += $line.EMPLOYEE_NUMBER + "," + $line.UNIQUE_ID + "," + $line.FIRST_NAME + "," + $line.LAST_NAME + "," + $line.SAMACCOUNT  + "`n"
		}
		else{
# I dont know how to add the "N" under the STATUS column	
		$b += $line.EMPLOYEE_NUMBER + "," + $line.UNIQUE_ID + "," + $line.FIRST_NAME + "," + $line.LAST_NAME + "," + $line.SAMACCOUNT + "`n"
		}
    }
    Out-File -FilePath $output -Encoding ASCII -InputObject $b
}

CheckADUser "C:\scripts\output\source.csv" "C:\scripts\Output\results.csv"

Open in new window

0
Comment
Question by:bndit
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:soostibi
ID: 35211142
Please try this.
Function CheckADUser($filename, $output) {
    Import-Csv $filename | ForEach-Object {
        $_ | Add-Member -PassThru -MemberType noteproperty -Value (
            if(Get-QADUser -samaccountName $line.SAMACCOUNT){"Y"}else{"N"}
        )
    } | Export-Csv -Path $output -Encoding ASCII -NoTypeInformation
}

Open in new window

0
 
LVL 13

Expert Comment

by:soostibi
ID: 35211162
Sorry, this one:
Function CheckADUser($filename, $output) {
    Import-Csv $filename | ForEach-Object {
        $_ | ForEach-Object {Add-Member -InputObject $_ -PassThru -MemberType noteproperty -Value @(
                if(Get-QADUser -samaccountName $_.SAMACCOUNT){"Y"}else{"N"}
            )}
    } | Export-Csv -Path $output -Encoding ASCII -NoTypeInformation
}

Open in new window

0
 
LVL 13

Accepted Solution

by:
soostibi earned 500 total points
ID: 35211182
Awfully sorry, but this is really the good one:
Function CheckADUser($filename, $output) {
    Import-Csv $filename | ForEach-Object {
        $_ | ForEach-Object {Add-Member -name Status -InputObject $_ -PassThru -MemberType noteproperty -Value $(
                if(Get-QADUser -samaccountName $_.SAMACCOUNT){"Y"}else{"N"}
            )}
    } | Export-Csv -Path $output -Encoding ASCII -NoTypeInformation
}

Open in new window

0
 
LVL 2

Author Comment

by:bndit
ID: 35211530
Thanks soostibi...that worked like a charm!
0
 
LVL 2

Author Closing Comment

by:bndit
ID: 35211535
Excellent!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now