支持资源

支持资源

我很茫然,想知道是否有人足够聪明又善良,能够帮助我跨过终点线。

我从 csv 中提取信息,然后搜索我们的 AD 并将相关信息输出到新的 csv 中。

我快到了,但是我需要将 A 列和 B 列中的组捆绑在一起,让它们在一行中运行。

例如

B0000180    A00000009
B0000180    A00000002
B0000180    A00000008
M0000140    A00000001
M0001040    A00000009
M0000140    A00000005
M0000140    A00000009
MD000049    A00000008
MD000049    A00000007
MD000049    A00000002
MD000049    A00000000
MDIA1069    A00000007
MDIA1069    A00000005
MDIA1140    A00000005
MDIA1140    A00000062
MDIA1140    A00000069
M1111144    A00000095
M2222260    A00000042
M2222260    A00000086

我希望看到的是

B0000180    A00000009 A00000002 A00000008
M0000140    A00000001 A00000009 A00000005 A00000009
MD000049    A00000008 A00000007 A00000002 A00000000
MDIA1069    A00000007 A00000005
MDIA1140    A00000005 A00000062 A00000069
M1111144    A00000095
M2222260    A00000042 A00000086

我目前拥有的脚本是

$csv = import-csv C:\ASM\IN\asm-data.csv 
$csv | foreach-object {
    $Class = $_.class_id
    $Instructor = $_.instructor
Get-ADGroupMember -Identity $Class$Instructor -Recursive | Get-ADUser -Properties employeeID | Select-Object @{n=”class_id”;e={$class}}, employeeID | Export-CSV C:\ASM\OUT\classes.csv -NoTypeInformation -Force -Append
}

答案1

无论是否有包含 [第一行] 列标题的起始示例 csv 数据(例如instructor, class_id),您都可以使用以下 PowerShell 逻辑来制定首选输出。

笔记:如果 Csv 文件确实已经包含列标题值,那么第一行将完全$csv = Import-Csv "C:\ASM\IN\asm-data.csv"省略-Header Instructor, Class_Id

  1. -Header Instructor, Class_Id 使用命令定义列标题值,Import-Csv以便它可以Group-Object与 哈希表一起Instructor使用。[PSCustomObject]

  2. Instructor将使用自定义数据对象,因此($_.group).'Instructor' | Select-Object -Unique不会列出重复的指导员。

  3. Class定义数据对象将使用空格作为分隔符来连接各个值(-join即)Class_Id'Class' = ($_.group).'Class_Id' -join " "

电源外壳

$csv = Import-Csv "C:\ASM\IN\asm-data.csv" -Header Instructor, Class_Id;
$groups = $csv | Group-Object Instructor;
$data = $groups | % {
    [PSCustomObject]@{
        'Instructor' = ($_.group).'Instructor' | Select-Object -Unique;
        'Class' = ($_.group).'Class_Id' -join " ";
    };
};
$data;
### $data | Format-Table -HideTableHeaders; ## use this to select data with no header columns

输出

Instructor Class                                  
---------- -----                                  
B0000180   A00000009 A00000002 A00000008          
M0000140   A00000001 A00000005 A00000009          
M0001040   A00000009                              
MD000049   A00000008 A00000007 A00000002 A00000000
MDIA1069   A00000007 A00000005                    
MDIA1140   A00000005 A00000062 A00000069          
M1111144   A00000095                              
M2222260   A00000042 A00000086               

PowerShell(新变体)

$csv = Import-Csv "C:\ASM\IN\asm-data.csv" -Header Instructor, Class_Id;
$groups = $csv | Group-Object Instructor;
$data = $groups | % {
    [PSCustomObject]@{
        'Instructor' = ($_.group).'Instructor' | Select-Object -Unique;
        'Class1' = ($_.group)[0].'Class_Id' -join ",";
        'Class2' = ($_.group)[1].'Class_Id' -join ",";
        'Class3' = ($_.group)[2].'Class_Id' -join ",";
        'Class4' = ($_.group)[3].'Class_Id' -join ",";
        'Class5' = ($_.group)[4].'Class_Id' -join ",";
    };
};
$tempCSV = "$Env:TEMP\~tempcsv.csv";
$data | Export-Csv $tempCSV -NoTypeInformation -Force;
$data = Import-Csv $tempCSV | FT;
$data;

输出

Instructor Class1    Class2    Class3    Class4    Class5
---------- ------    ------    ------    ------    ------
B0000180   A00000009 A00000002 A00000008                 
M0000140   A00000001 A00000005 A00000009                 
M0001040   A00000009                                     
MD000049   A00000008 A00000007 A00000002 A00000000       
MDIA1069   A00000007 A00000005                           
MDIA1140   A00000005 A00000062 A00000069                 
M1111144   A00000095                                     
M2222260   A00000042 A00000086  

支持资源

相关内容