Solved

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

Posted on 2011-03-24
5
1,452 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks soostibi...that worked like a charm!
0
 
LVL 2

Author Closing Comment

by:bndit
Comment Utility
Excellent!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
Synchronize a new Active Directory domain with an existing Office 365 tenant
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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

10 Experts available now in Live!

Get 1:1 Help Now