【问题标题】:How to turn array into multiple rows?如何将数组变成多行?
【发布时间】:2016-08-12 18:42:22
【问题描述】:

我正在使用 Powershell 来显示所有组策略以及它们所链接的内容。这需要 GroupPolicy 模块(Import-Module GroupPolicy)和以下脚本(感谢 Mike Frobbins)完全满足我的需要,但输出以以下格式显示:

“GPOName”、“LinksTo”、“已启用”

"默认组策略","Servers,Computers,Texas","true,true,true"

我需要它看起来像这样:

“GPOName”、“LinksTo”、“已启用”

"默认组策略","服务器","true"
"默认组策略","计算机","true"
"默认组策略","Texas","true"

任何帮助将不胜感激。我一直试图让它工作 2 天,但我无法弄清楚。我发现其他文章涵盖了该主题,但解决方案似乎不适用于此方法。

function Get-GPOLink {

[CmdletBinding()]
param (
    [Parameter(Mandatory,
               ValueFromPipeline,
               ValueFromPipelineByPropertyName)]
    [Alias('DisplayName')]
    [string[]]$Name
)

PROCESS {

    foreach ($n in $Name) {            
        $problem = $false

        try {
            Write-Verbose -Message "Attempting to produce XML report for GPO: $n"

            [xml]$report = Get-GPOReport -Name $n -ReportType Xml -ErrorAction Stop
        }
        catch {
            $problem = $true
            Write-Warning -Message "An error occured while attempting to query GPO: $n"
        }

        if (-not($problem)) {
            Write-Verbose -Message "Returning results for GPO: $n"

            [PSCustomObject]@{
                'GPOName' = $report.GPO.Name
                'LinksTo' = $report.GPO.LinksTo.SOMPath
                'Enabled' = $report.GPO.LinksTo.Enabled
            }
        }
    }
}
}

【问题讨论】:

    标签: arrays powershell


    【解决方案1】:

    您可以将[PSCustomObject] 包装在一个循环中以迭代SOMPath 的所有实例。

    function Get-GPOLink {
    
    [CmdletBinding()]
    param (
        [Parameter(Mandatory,
                   ValueFromPipeline,
                   ValueFromPipelineByPropertyName)]
        [Alias('DisplayName')]
        [string[]]$Name
    )
    
    PROCESS {
    
        foreach ($n in $Name) {            
            $problem = $false
    
            try {
                Write-Verbose -Message "Attempting to produce XML report for GPO: $n"
    
                [xml]$report = Get-GPOReport -Name $n -ReportType Xml -ErrorAction Stop
            }
            catch {
                $problem = $true
                Write-Warning -Message "An error occured while attempting to query GPO: $n"
            }
    
            if (-not($problem)) {
                Write-Verbose -Message "Returning results for GPO: $n"
    
                ForEach ($LinkTo in $report.GPO.LinksTo) {
                    [PSCustomObject]@{
                        'GPOName' = $report.GPO.Name
                        'LinksTo' = $LinkTo.SOMPath
                        'Enabled' = $LinkTo.Enabled
                    }
                }
            }
        }
    }
    }
    

    编辑:更新为遍历 LinksTo 记录以避免多个 Enabled 响应。

    【讨论】:

    • 你可能让我很开心。这有效,但现在“启用”列在两行都显示“真真”。有没有办法让“已启用”列中的相应项目也显示在相应的行上?
    • 您会惊讶地发现,这里的答案经常是对某人过度思考的问题的简单解决方案:)
    猜你喜欢
    • 2018-05-16
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    相关资源
    最近更新 更多