Output department and direct reports

不想你离开。 提交于 2019-12-12 06:08:15

问题


I am trying to create an "initial" text file that will hold a script run of all users + department + direct reports. My next step after making this file is to create another file the same way but compare it to the original to see if the department for the users ever changed. (not sure yet how to compare the department value just yet)

My current issue is that the department, even though the process is identical to another program I have made in the past, won't print it. Furthermore, when it prints my direct reports it prints only the first one with the whole extension of CN=..., OU=... etc.

I want it to print this way:

username | Department(extensionAttribute14) | Direct Reports (as a single string)
we38432 | IT-Security | cm03456: 04555a: ....etc

My original script used this code for department:

$deps = Get-Aduser -filter {name -like *} -Properties name, extensionAttribute14 | Select name, extensionAttribute14 | Export-CSV $listing -notypeinformation

and this worked. I tried the {name -like *} but that gave me errors in my current program. I know the Export-CSV makes it work but I can't use this format anymore.

for the direct reports my original was this:

foreach ($ID in $directReports){
    if ($ID -ne $Null){
    $directreports = get-aduser $ID
    $directreports.name | Out-File $output -Append 
}

This code printed line by line the direct reports but I want them all listed in the same excel cell when I send it there.

I have printed a listing of all the members in the past using ":" and it worked but it is not the case with the direct reports listing. I just get errors when I use this format from my other program:

foreach ($member in $empty.members){
    $string = $member.substring(3,$member.indexof(",")-3)
    $members = $members + ":" + $string
}

I hope someone can help me with my two issues.

Import-Module ActiveDirectory

$documentOld = "C:\Temp\Old_Supervisor_list_mo_yyyy.txt"

Clear-Content $documentOld

$Header = `
"User ID" <#+ "|" + `
"Department" + "|" + `
"Direct Reports"#>

$Header | Out-File $documentOld -Append

$Users = Get-AdUser -Filter * -Properties name, Enabled, Manager, extensionAttribute14 | Select Enabled, name, Manager, extensionAttribute14

foreach ($user in $Users){
if ($user.enabled –eq $true) {
    $name = $user.name
    $directReports = Get-ADUser -Identity $name -Properties directreports | Select -ExpandProperty directreports
    $department = $user.extensionAttribute14


foreach ($ID in $directReports){
if ($ID -ne $Null){
            $directreports = get-aduser $ID
 #           $string = $directreports + ":"

}#end if $ID
}#end foreach $ID

$listing = `
$name  + "|" +  $deparment + "|" + $directreports#$string

$listing | Out-File $documentOld -Append

}# end if
}# end foreach $user

回答1:


Let see if we can make this a little easier and efficient.

Import-Module ActiveDirectory

$documentOld = "C:\Temp\Old_Supervisor_list_mo_yyyy.txt"

$Users = Get-AdUser -Filter *  -Properties name,Enabled,Manager,extensionAttribute14 | Where-Object{$_.Enabled}

$Users | ForEach-Object{
    $props = @{
        Name = $_.Name
        Department = $_.extensionAttribute14
        DirectReports = ($_.Manager | Where-Object{$_} | ForEach-Object{Get-Aduser $_ | Select-object -ExpandProperty Name}) -join ":"
    }

    New-Object -TypeName psobject -Property $props

} | Select-Object Name,Department,DirectReports | Export-CSV -Delimiter "|" -NoTypeInformation -Path $documentOld

First we get all the users from your directory with Get-AdUser -Filter * taking all the properties outside the norm that we want. Since you just wanted accounts that are enabled we filter those out now with Where-Object{$_.Enabled}.

The fun part is creating the custom object array ( which is necessary for input for Export-CSV). Create a small hashtable called $props where we set the properties by their friendly names. The special one being DirectReports where we take all the users manager DN's ( Assuming they have one where is what Where-Object{$_} does by filtering out nulls/empty strings.) and use Get-Aduser to get there names. Since you could have more than one manager an array is most likely returned we use -join to ensure only a single string is given for the DirectReports property. That property collection is created for every user and it is then used to create a New-Object which is sent to the output stream.

The Select-Object that follows is just to ensure the order of columns in the CSV that is created. No need for making a CSV file with lots of Out-Files when Export-CSV and -Delimiter "|" will do the hard work for you.



来源:https://stackoverflow.com/questions/28815126/output-department-and-direct-reports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!