Ric Ric - 3 months ago 6
PowerShell Question

How to turn array into multiple rows?

I'm using Powershell to show all Group Policies and what they are linked to. This requires the GroupPolicy module (Import-Module GroupPolicy) and the following script (credit to Mike Frobbins) accomplishes exactly what I need, but the output is shown in the following format:


"GPOName","LinksTo","Enabled"


"Default Group Policy","Servers,Computers,Texas","true,true,true"


And I need it to to look like this:



"GPOName","LinksTo","Enabled"


"Default Group Policy","Servers","true"

"Default Group Policy","Computers","true"

"Default Group Policy","Texas","true"


Any help would be appreciated. I've been trying to get this to work for 2 days and I cannot figure it out. I've found other articles that have covered this topic but the solutions didn't seem to apply to this method.

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
}
}
}
}
}

Answer

You could wrap your [PSCustomObject] in a loop to iterate all all instances of 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
                }
            }
        }
    }
}
}

Edit: Updated to iterate through LinksTo records to avoid multiple Enabled responses.

Comments