?
Solved

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

Posted on 2011-03-24
5
Medium Priority
?
1,703 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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

719 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