Wednesday, March 8, 2017

Who Manages Who?

I work for a medium size company.  There are a number of office and production employees.  Sometimes it is a challenge to determine the manager of office personnel.  Below is a script I created that reads information from Active Directory and generates a .csv file that contains the employee's name and their manager:
<#



.SYNOPSIS

Script that generates employee/manager report



.DESCRIPTION

This script stores all user accounts from specific OU into a variable. This information is used to create a report that contains

an employee and the employee's manager



.NOTES

Name: employee_manager.ps1

Author: Mike Egan

DateCreated: 2015-12-4

Version: 1.1

#VERSION HISTORY

#

# Version 1.2 (March 8, 2017)

# Changed the $Results declaration to include a ',' between $uName and $uMgr

# Added $filedata = import-csv $outfile -header Employee, Manager so the report would have

# the proper headings

# Added $filedata | export-csv $outfile -NoTypeInformation so the output file had the proper

# headings





#>
 
$userlist = get-aduser -filter * -properties * -searchbase "OU=<organizational unit>,DC=<domain>,DC=<domain>" | select name,manager #| export-csv employee_manager.csv

$outfile = "c:\downloads\test.csv"

foreach ($usr in $userlist) {

#write-host $usr.name

if (!($usr.manager -eq $null)){

$trimmed_mgr = $usr.manager.substring(3)

$var1 = $trimmed_mgr.split(",")

$uName = $usr.name

$uMgr = $var1[0]

$results = "$uName,$uMgr"

} $results | out-file -append $outfile

$results = $null # End IF

} # End ForEach

$filedata = import-csv $outfile -Header Employee , Manager

$filedata | export-csv $outfile -NoTypeInformation

In the get-aduser command, an example of a -searchbase would be "OU=users,DC=Microsoft,DC=Net"

The output of the script will be the c:\downloads\test.csv.  NOTE: you may want to delete this file each time you run this script or the file will be full of duplicate information.

As you can see I commented out write-host $usr.name.  I used this command during the building and testing of this script.  You can remove the line if you like.

I hope you find this script helpful

No comments:

Post a Comment