Thursday, April 30, 2015

Powershell: ARS : How to get user details from AD using ARS script

I had a situation where I wanted to pull the following details from ARS which I could not find in SCCM.

I extracted a report from SCCM from which I got the AD username of users and I ran it against the AD using ARS script to fetch the details and get it in a CSV. Then I combined both excel files to get all details.
---------------------------------------------------------------------------------------------------------------------

#This program pulls user data from ARS.

#input is kept in csv file in following variable, title should be "UserName" , it contains the AD usernames of the users.

$source=".\users2.csv"

#Below file will have the exported user details

$outfile=".\UserDetails-Exported.csv"

$list=Import-Csv $source

$results = @()

$count=0;

#Write the header to outfile.

Write-Output "UserID,LastName,FirstName,Email,Location" | Out-File $outfile -Encoding utf8 -Append

$list | foreach{

$username=$_.UserName;

$count+=1;

if($username.length -gt 3)

{

  get-qaduser $username |  foreach{$lname=$_.lastname;$fname=$_.firstname;$email=$_.email;$uid=$_.samaccountname;$location=$_.l;  }

  write-host $count

  Write-Host "$uid,$lname,$fname,$email,$location"

  Write-Output "$uid,$lname,$fname,$email,$location" | Out-File $outfile -Encoding utf8 -Append

  $details = @{          

                UserID=$uid

                FirstName= $fname

                LastName= $lname

                Email=$email              

                Location=$location

            }                          

  $results += New-Object PSObject -Property $details

}

else

{

  $lname="No data found";

  $fname="No data found";

  $email="No data found";

  $uid="No data found";

  $location="No data found"

  $details = @{          

                UserID=$uid

                LastName= $lname              

                FirstName= $fname

                Email=$email              

                Location=$location

            }

Write-Output "$uid,$lname,$fname,$email,$location" | Out-File $outfile -Encoding utf8 -Append

}

}

3 comments:

  1. Iron Spade - Titanium Spade - Titanium Spade - Titanium
    In Tithium-Argentine habanero, this is the most robust and delicious titanium dioxide sunscreen habanero in your mouth! It is microtouch titanium a titanium tv alternative medium-medium-hot toaks titanium 750ml pot pepper, titanium rod in femur complications which you can't beat. Rating: 5 · ‎1 review · ‎$13.99 · ‎In stock

    ReplyDelete