I have a list of employees with their Employee ID, Name, Manager Name and Manager ID. I’m trying to run a Powershell script that will link the employee to their manager, and that manager, to their manager, etc, but my code isn’t giving me the results I’m hoping for. Here’s an example of my CSV:<\/p>\n
ID,FirstName,LastName,ManagerID\n5,Smith,John,8\n8,Jones,James,332\n332,Schmidt,Johan,15\n10,Simmons,Richard,3032\n15,MakeIt,Willie,3032\n3032,Wont,Betty,4507\n4507,Bunns,Seymore,9575\n9575,TooFar,Eileen,8988\n8988,Beatem,Dewey,0\n<\/code><\/pre>\n
Advertisement
In this example, John Smith, reports to James Jones, who reports to Johan Scmidt, who reports to Willie MakeIt, who reports to Betty Wont, who reports to Seymore Bunns, who reports to Eileen TooFar, who reports to Dewey Beatem. Since Richard Simmons also reports to Willie MakeIt, it should reflect that in the output.<\/p>\n
Advertisement
This is an example of my script.<\/p>\n
$Employees = Import-CSV -Path \"C:\\Scripts\\OrgChart\\ActiveEmployees.csv\"\n$Employees | Get-Member\nFunction Add-ManagerLevel([Object[]]$Employees) {\n\n Function Get-Level($ManagerID, [INT]$Level = 0) {# 0 is the top of heirarchy\n $Manager - $Employees | Where-Object {_.Id -eq $ManagerID}\n IF (!$Manager) {$Level} Else {Get-level $Manager.ManagerID ($Level + 1)}\n }\n\n $Employees | ForEach-Object {\n $_ | Add-member \"Level\" (Get-level $_.ManagerID) -PassThru\n }\n}\n$Employees | Export-Csv -Path \"C:\\Scripts\\OrgChart\\OrgChart.csv\" -NoTypeInformation -Encoding UTF8\n<\/code><\/pre>\nI get this output from the script:<\/p>\n
PS C:\\Scripts\\OrgChart> .\\Get-OrgChart.ps1\n\n TypeName: System.Management.Automation.PSCustomObject\n\nName Member Type Definition \n---- ---------- ---------- \nEquals Method bool Equals(System.Object obj)\nGetHashCode Method int GetHashCode() \nGetType Method type GetType() \nToString Method string ToString() \nFirstName NoteProperty string FirstName=Alexander \nID NoteProperty string ID=5 \nLastName NoteProperty string LastName=Davis \nManagerID NoteProperty string ManagerID=8\n<\/code><\/pre>\nWhat am I doing wrong? Please help if you can. We can make this work in SQL, but we’re trying to do it in PowerShell.<\/p>","upvoteCount":3,"answerCount":15,"datePublished":"2021-12-02T15:47:26.000Z","author":{"@type":"Person","name":"twyrch","url":"https://community.spiceworks.com/u/twyrch"},"acceptedAnswer":{"@type":"Answer","text":"
Just build the directed graph from the data and traverse it a couple of times to make a lookup hash table and assign the levels. Afterwards you can use the same traversal technique from the top level to generate the report in any way you want.<\/p>\n
$testData = @'\nID,FirstName,LastName,ManagerID\n5,Smith,John,8\n8,Jones,James,332\n332,Schmidt,Johan,15\n10,Simmons,Richard,3032\n15,MakeIt,Willie,3032\n3032,Wont,Betty,4507\n4507,Bunns,Seymore,9575\n9575,TooFar,Eileen,8988\n8988,Beatem,Dewey,0\n'@ | ConvertFrom-Csv |\nForEach-Object {\n [PSCustomObject][ordered]@{\n ID = $_.ID\n FirstName = $_.FirstName\n LastName = $_.LastName\n ManagerID = $_.ManagerID\n DirectReports = @()\n Level = 0\n }\n}\n\n$lookUp = @{}\nforeach ($employee in $testData) {\n $lookUp.Add($employee.ID, $employee)\n}\n\n$rootCandidate = $null\nforeach ($employee in $testData) {\n if ($lookUp.ContainsKey($employee.ManagerID)) {\n $lookUp[$employee.ManagerID].DirectReports += $employee\n }\n else {\n $rootCandidate = $employee\n }\n}\n\nfunction Set-Level( $employee, $level) {\n $employee.Level = $level\n foreach ($report in $employee.DirectReports) {\n Set-Level $report ($level+1)\n } \n}\n\nSet-Level $rootCandidate 1\n$lookUp.Values\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2021-12-02T21:37:00.000Z","url":"https://community.spiceworks.com/t/building-an-orgchart-in-powershell/818574/8","author":{"@type":"Person","name":"tulioarends","url":"https://community.spiceworks.com/u/tulioarends"}},"suggestedAnswer":[{"@type":"Answer","text":"I have a list of employees with their Employee ID, Name, Manager Name and Manager ID. I’m trying to run a Powershell script that will link the employee to their manager, and that manager, to their manager, etc, but my code isn’t giving me the results I’m hoping for. Here’s an example of my CSV:<\/p>\n
ID,FirstName,LastName,ManagerID\n5,Smith,John,8\n8,Jones,James,332\n332,Schmidt,Johan,15\n10,Simmons,Richard,3032\n15,MakeIt,Willie,3032\n3032,Wont,Betty,4507\n4507,Bunns,Seymore,9575\n9575,TooFar,Eileen,8988\n8988,Beatem,Dewey,0\n<\/code><\/pre>\nIn this example, John Smith, reports to James Jones, who reports to Johan Scmidt, who reports to Willie MakeIt, who reports to Betty Wont, who reports to Seymore Bunns, who reports to Eileen TooFar, who reports to Dewey Beatem. Since Richard Simmons also reports to Willie MakeIt, it should reflect that in the output.<\/p>\n
This is an example of my script.<\/p>\n
$Employees = Import-CSV -Path \"C:\\Scripts\\OrgChart\\ActiveEmployees.csv\"\n$Employees | Get-Member\nFunction Add-ManagerLevel([Object[]]$Employees) {\n\n Function Get-Level($ManagerID, [INT]$Level = 0) {# 0 is the top of heirarchy\n $Manager - $Employees | Where-Object {_.Id -eq $ManagerID}\n IF (!$Manager) {$Level} Else {Get-level $Manager.ManagerID ($Level + 1)}\n }\n\n $Employees | ForEach-Object {\n $_ | Add-member \"Level\" (Get-level $_.ManagerID) -PassThru\n }\n}\n$Employees | Export-Csv -Path \"C:\\Scripts\\OrgChart\\OrgChart.csv\" -NoTypeInformation -Encoding UTF8\n<\/code><\/pre>\nI get this output from the script:<\/p>\n
PS C:\\Scripts\\OrgChart> .\\Get-OrgChart.ps1\n\n TypeName: System.Management.Automation.PSCustomObject\n\nName Member Type Definition \n---- ---------- ---------- \nEquals Method bool Equals(System.Object obj)\nGetHashCode Method int GetHashCode() \nGetType Method type GetType() \nToString Method string ToString() \nFirstName NoteProperty string FirstName=Alexander \nID NoteProperty string ID=5 \nLastName NoteProperty string LastName=Davis \nManagerID NoteProperty string ManagerID=8\n<\/code><\/pre>\nWhat am I doing wrong? Please help if you can. We can make this work in SQL, but we’re trying to do it in PowerShell.<\/p>","upvoteCount":3,"datePublished":"2021-12-02T15:47:26.000Z","url":"https://community.spiceworks.com/t/building-an-orgchart-in-powershell/818574/1","author":{"@type":"Person","name":"twyrch","url":"https://community.spiceworks.com/u/twyrch"}},{"@type":"Answer","text":"
Welcome<\/p>\n
If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n