尝试建立具体办公室的组织结构图

问题描述 投票:0回答:1

我对 powershell 还很陌生,所以如果这是非常基本的东西,我深表歉意。我们收到一个请求,要求创建一个组织结构图,该结构图将导出为 csv,然后用于我们一些办公室的其他应用程序,其中包括办公室主管、办公室经理,最后是该地区的副总裁。我已经通过经理用这个脚本来拉动办公室了:

$supervisors = (Get-ADUser -Filter "Title -Like '*supervisor*'" -Properties Office, Manager | Where-Object Office -notlike "*Corporate Office*" | select Office, Name, @{Name="Manager"; Expression={(Get-ADUser $_.manager).name}})

问题是我们需要添加最后一栏,其中包含每个办公室的副总裁,即经理的汇报对象。另外一些分支机构的主管直接向副总裁本人汇报,因此他们被列在经理部分中,因此我们需要将该名称转移到新列中。作为参考,这里是信息引入的布局。我尝试将所有信息添加到一个数组中,并尝试 -getaduser 仅从管理器部分提取信息。我现在有点迷失了,不知道该去哪里。

办公室主管经理


arrays powershell charts active-directory
1个回答
0
投票

如果我理解正确的话,你应该使用循环而不是

Select-Object
,这样会更容易阅读代码并且避免查询同一用户两次。添加一些注释来帮助您遵循逻辑。

$getADUserSplat = @{
    LDAPFilter = '(&(title=*supervisor*)(!title=*Corporate Office*))'
    Properties = 'Office', 'Manager'
}

$supervisors = Get-ADUser @getADUserSplat | ForEach-Object {
    # get the manager's `.Name` and try to get the manager's `Manager`
    $manager = Get-ADUser $_.Manager -Properties Manager
    # if the manager's `Manager` attribute is populated
    if ($manager.Manager) {
        # use the manager's `Manager` name
        $vp = (Get-ADUser $manager.Manager).Name
    }
    else {
        # else, it isn't populated
        # so fall back to the same value used for `Manager` property
        $vp = $manager.Name
    }

    [pscustomobject]@{
        Office  = $_.Office
        Name    = $_.Name
        Manager = $manager.Name
        VP      = $vp
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.