Solved

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

Posted on 2011-03-24
5
1,500 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This article will help you understand what HashTables are and how to use them in PowerShell.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

815 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

9 Experts available now in Live!

Get 1:1 Help Now