输出部门和直接报告 [英] Output department and direct reports

查看:147
本文介绍了输出部门和直接报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个初始"文本文件,该文件将包含所有用户+部门+直接报告的脚本运行.制作此文件后,我的下一个步骤是以相同的方式创建另一个文件,但是将其与原始文件进行比较,以查看用户部门是否有所更改. (尚不确定如何比较部门价值)

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)

我当前的问题是,即使该过程与我过去制作的另一个程序相同,该部门也不会打印它.此外,当它打印我的直接报告时,它只打印第一个扩展名为CN = ...,OU = ...等的完整报告.

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

,并且有效.我尝试了{name -like *},但是这在我当前的程序中给了我错误.我知道Export-CSV可以使它工作,但是我不能再使用这种格式了.

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 
}

此代码逐行打印直接报告,但我希望在将其发送到同一Excel单元格时将它们全部列出.

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

推荐答案

让我们看看是否可以使它变得更简单有效.

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

首先,我们使用Get-AdUser -Filter *从您的目录中获取所有用户,并获取我们想要的规范之外的所有属性.由于您只想要启用的帐户,我们现在使用Where-Object{$_.Enabled}过滤掉这些帐户.

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}.

有趣的部分是创建自定义对象数组(这是Export-CSV输入所必需的).创建一个名为$props的小型哈希表,在其中我们通过属性的友好名称进行设置.特殊的是DirectReports,其中存放了所有用户管理器DN(假设他们在其中一个位置是Where-Object{$_}通过过滤出空字符串/空字符串来完成的工作.)并使用Get-Aduser来获取名称.因为您可能有多个管理器,所以很可能返回一个数组,因此我们使用-join确保DirectReports属性仅给出一个字符串.将为每个用户创建该属性集合,然后将其用于创建New-Object并将其发送到输出流.

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.

后面的Select-Object只是为了确保所创建的CSV中的列顺序.当Export-CSV-Delimiter "|"将为您完成艰苦的工作时,无需制作包含许多Out-File的CSV文件.

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.

这篇关于输出部门和直接报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆