更改脚本以不同方式导出组和嵌套对象 [英] Change the script to export Groups and nested objects differently

查看:31
本文介绍了更改脚本以不同方式导出组和嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个类似的脚本:

I created a similiar script like that:

$Groups = Get-QADGroup 
$Result = @()
$Groups | ForEach-Object {
    $Group = $_
    $Members = Get-QADGroupMember $Group -Indirect | ? objectClass -eq "user"
    $Obj = '' | Select-Object -Property Name, Members 
    $Obj.Name = $Group.Name
    $Obj.Members = ($Members | % {$_.SamAccountName + "_" + 'Test'})
    $Result += $Obj
}
$Result | Export-Csv -Path C:\Temp\groups.csv -NoTypeInformation -Encoding Unicode -Delimiter ";"

输出看起来像这样(示例数据):

The output looks something like this (example data):

"Name";"Members"
"RootGroup01";"Subuser01_Test";"Subuser02_Test";"Subuser03_Test"
"RootGroup02";"Subuser02_Test"
"RootGroup03";"Subuser01_Test";"Subuser02_Test";"Subuser04_Test"; 

是否可以更改脚本,我会得到这样的结果?:

Is it possible to change the script, that I get something like this?:

"RootGroup01";"RootGroup02";"RootGroup03"
"Subuser01_Test";"Subuser02_Test";"Subuser01_Test"
"Subuser02_Test";;"Subuser02_Test"
"Subuser03_Test";;"Subuser04_Test"

组名应该是标题,所属用户在右列中.如果没有用户,则列单元格保持为空.

The group names should be the header and the belonging users are in the right column. If there is no user, the column cell just stays empty.

推荐答案

你可以编写这样的函数来转置你的数据:

You can write such function to transpose your data:

function Transpose-Data{
    param(
        [String[]]$Names,
        [Object[][]]$Data
    )
    for($i = 0;; ++$i){
        $Props = [ordered]@{}
        for($j = 0; $j -lt $Data.Length; ++$j){
            if($i -lt $Data[$j].Length){
                $Props.Add($Names[$j], $Data[$j][$i])
            }
        }
        if(!$Props.get_Count()){
            break
        }
        [PSCustomObject]$Props
    }
}

然后通过以下方式调用它:

Then you invoke it in the following way:

$Groups = @(Get-QADGroup)
$Members = @(
    $Groups | ForEach-Object {
        ,@(
            Get-QADGroupMember $_ -Indirect |
            ? objectClass -eq user |
            % {$_.SamAccountName + "_" + 'Test'}
        )
    }
)
Transpose-Data $Groups.Name $Members |
Export-Csv -Path C:\Temp\groups.csv -NoTypeInformation -Encoding Unicode -Delimiter ";"

这篇关于更改脚本以不同方式导出组和嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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