Solved

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

Posted on 2011-03-24
5
1,596 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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