如何处理具有一些双重值的哈希表?使用字符串

时间:2018-03-05 23:37:12

标签: powershell active-directory hashtable

我的任务是在AD中查找用户部门,然后将其与查找CSV交叉引用文件中的部门手册相匹配。他们为每个部门编写了1个代码,映射到1个手册,但某些部门可能会获得2个不同的手册。

文件1:

Code    Manual
A   Administrative
B   Ambulatory Care
C   Anatomic Pathology & Clinical Laboraties

文件2:

AD Departments  Code
300P RENEWAL    A3
ACCOUNTING OPERATIONS   A11
ACCOUNTS PAYABLE    A11
ACCREDITATION   A
ADVANCED LUNG DISEASE   B
AGING ADULT SERVICES    A3
ANESTHESIA  A6
APP - INPATIENT A3
B2  A3
BENEFITS MANAGEMENT A11
BLOOD TRANSFUSION SERVICES  A3
BMT SERVICE LINES   B
Business Development    V
BUSINESS OPS&STRATEGIC INIT V
BW DIGESTIVE HEALTH CENTER  B, V

如果部门与文件一对一地映射,则此代码可以正常工作,但如果AD部门有2个代码(对于2个不同的手册),则它似乎只抓取第一个。有没有一种简单的方法可以列出这两种情况,比如上一个BW DIGESTIVE HEALTH CENTER,他们需要手册B和V?理想情况下,$DeptMan可以在需要时列出2个值。

这是我到目前为止所拥有的:

$Depts | ForEach-Object {
    $Table1[$_.'AD Departments'] = $_
}

$Manuals | ForEach-Object {
    $Table2[$_.Code] = $_
}

$Managers = Get-ADUser -Filter {some filter} -pr SamaccountName,GivenName,Initials, SN,Company,Title,Department

foreach ($Man in $Managers) {
    $Dept    = $Man.Department 
    $Code    = $Table1.Item("$Dept")
    $CodeMap = $Code.code
    $Manual  = $Table2.Item("$CodeMap")
    $DeptMan = $Manual.Manual

    $Man |
        select @{Name="SID";Expression={$Man.SamAccountName}},
            @{Name="FirstName";Expression={$Man.givenName}},
            @{Name="Initials";Expression={$Man.Initials}},
            @{Name="LastName";Expression={$Man.sn}},
            @{Name="Department";Expression={$Man.Department}},
            @{Name="Email";Expression={$Man.Mail}},
            @{Name="Manual";Expression={$DeptMan}} |
        Export-Csv C:\output.csv -NoType -Append
}

1 个答案:

答案 0 :(得分:0)

好的,我终于明白了。您必须重置foreach循环中的变量,并将$ codes更改为字符串,否则它们不会计算为True或False。一旦我进入If块,我只是重新创建了新的手动变量,它似乎工作。我在初始脚本开头发布的所有其他内容都没有改变。希望这有助于某人。

foreach ($Man in $Managers) {

    $DeptMan = $Null
    $DeptMan1 = $Null

    $Dept = $Man.Department 

    $Code = $Table1.item("$Dept")

    [string]$CodeMap = $code.code

    $Manual = $Table2.item("$CodeMap")

    $DeptMan = $Manual.Manual

    if ($CodeMap -like "*,*") {$CodeMap1 = $code.code.Split(", ",[System.StringSplitOptions]::RemoveEmptyEntries) 

                                [string]$CodeMap2 = $CodeMap1[0] 

                                [string]$CodeMap3 = $CodeMap1[1]

                                $Manual1 = $Table2.item("$CodeMap2")

                                $Manual2 = $Table2.item("$CodeMap3")

                                $DeptMan = $Manual1.Manual

                                $DeptMan1 = $Manual2.Manual

                                }

   $Man | select @{Name="SID";Expression={$Man.SamAccountName}},
                 @{Name="FirstName";Expression={$Man.givenName}},
                 @{Name="Initials";Expression={$Man.Initials}},
                 @{Name="LastName";Expression={$Man.sn}},
                 @{Name="Department";Expression={$Man.Department}},
                 @{Name="Email";Expression={$Man.Mail}},
                 @{Name="Manual1";Expression={$DeptMan}},
                 @{Name="Manual2";Expression={$DeptMan1}}| export-csv C:\Scripts\1-Manuals_$((Get-Date).ToString('MM-dd-yyyy')).csv -NoTypeInformation -append 


    }