Warning: mysqli_result::fetch_assoc() expects exactly 0 parameters, 1 given in /var/www/powershellpower.com/public_html/page_parts/page.php on line 78

Warning: Use of undefined constant page_id - assumed 'page_id' (this will throw an Error in a future version of PHP) in /var/www/powershellpower.com/public_html/page_parts/page.php on line 92

Warning: Use of undefined constant synopsis - assumed 'synopsis' (this will throw an Error in a future version of PHP) in /var/www/powershellpower.com/public_html/page_parts/page.php on line 93

Active directory : How to get a list of all active directory users with direct reports

In this tutorial we find all users who have direct reports to them, in theory we should be able to find these users by their job role, but this would only work if we had a well-maintained AD but this is so often not the case

2
Warning: mysqli_result::fetch_assoc() expects exactly 0 parameters, 1 given in /var/www/powershellpower.com/public_html/page_parts/page.php on line 156

Warning: Use of undefined constant text - assumed 'text' (this will throw an Error in a future version of PHP) in /var/www/powershellpower.com/public_html/page_parts/page.php on line 185
Import the users from AD

The first step is to extract all the users of interest and store them in a variable. In my search I used the -SearchBase command to target my ‘Sites’ OU. You can leave this Section out if you want to search your entire domain


$managers = Get-ADUser -Filter * -Properties name, directReports | SORT name



Warning: Use of undefined constant text - assumed 'text' (this will throw an Error in a future version of PHP) in /var/www/powershellpower.com/public_html/page_parts/page.php on line 185
Create an empty array variable

Next we need to create an empty array to store all the entries we find.


$newman = @()



Warning: Use of undefined constant text - assumed 'text' (this will throw an Error in a future version of PHP) in /var/www/powershellpower.com/public_html/page_parts/page.php on line 185
Process users in foreach loop

Now we have our variables set up we need to run them through a foreach loop
$staff : This collects any users that are stored in the ‘directreports’ field in AD
$count : Counts the number (if any) of people that report to that person


foreach ($man in $managers){
$staff = $man.directReports
$count = $staff.Count



Warning: Use of undefined constant text - assumed 'text' (this will throw an Error in a future version of PHP) in /var/www/powershellpower.com/public_html/page_parts/page.php on line 185
Create an if statement

Within the foreach loop we create an if statement which says 'if the variable called $count has any number greater than 0 add the name stored in $man variale into our new variable called $newman'. The thoery here is that if the user has 0 direct reports to him we are not interested in this user.


if ($count -gt 0){
$newman += $man.SamAccountName
}
}



Warning: Use of undefined constant text - assumed 'text' (this will throw an Error in a future version of PHP) in /var/www/powershellpower.com/public_html/page_parts/page.php on line 185
The full script

$managers = Get-ADUser -Filter * -SearchBase "OU=sites,DC=barrett,DC=Local" -Properties name, directReports | SORT name
$newman = @()
cls
foreach ($man in $managers){
$staff = $man.directReports
$count = $staff.Count

if ($count -gt 0){
$newman += $man.SamAccountName
}
}


Cut & Paste


Comments

Notice: Undefined variable: commentno in /var/www/powershellpower.com/public_html/page_parts/page.php on line 335
// Collect comments ''
Fatal error: Uncaught Error: Call to a member function fetch_assoc() on bool in /var/www/powershellpower.com/public_html/page_parts/comments.php:9 Stack trace: #0 /var/www/powershellpower.com/public_html/page_parts/page.php(343): include() #1 /var/www/powershellpower.com/public_html/pages/activedirectory_How_to_get_a_list_of_all_active_directory_users_with_direct_reports.php(2): include('/var/www/powers...') #2 {main} thrown in /var/www/powershellpower.com/public_html/page_parts/comments.php on line 9