Highlighted
Enthusiast
Enthusiast

Get-DrsClusterGroup - CSV Export

Jump to solution

I'd like to be able to get the results in columns in CSV.

pastedImage_4.png

$fileName = $env:Temp + '\AffinityRules.csv'

Get-DrsClusterGroup | Select @{N="GroupName";E={$_.Name}}, Cluster, @{N="Member";E={$_.Member.Name}} | Export-Csv -Path $fileName -NoTypeInformation -UseCulture

Send-MailMessage -From "vca002@inc.com" -To "my_email@inc.com" -Attachments $fileName -Subject "AffinityRules" -SmtpServer "smtp.inc.com"

The snippet here almost does the same thing, it produces the result but the Member property, when exported in CSV, adds the values all in one cell for a group. Could we have one member per cell in columns?

1 Solution

Accepted Solutions
Highlighted
User Moderator
User Moderator

Due to a "feature" in Export-Csv it uses the first row of the exported array to determine how many columns there will be in the CSV.

This can be bypassed by sorting the array in such a way that the row with the most members comes first.

$fileName = $env:Temp + '\AffinityRules.csv'

Get-DrsClusterGroup -PipelineVariable group |

ForEach-Object -Process {

    $obj = [ordered]@{

        GroupName = $group.Name

        Cluster = $group.Cluster.Name

    }

    $i = 1

    $group.Member| ForEach-Object -Process {

        $obj.Add("Member$($i)",$_.Name)

        $i++

    }

    New-Object -TypeName PSObject -Property $obj

} | Sort-Object -Property {(Get-Member -InputObject $_ -MemberType NoteProperty).Count} -Descending |

Export-Csv -Path $fileName -NoTypeInformation -UseCulture


Send-MailMessage -From "vca002@inc.com" -To "my_email@inc.com" -Attachments $fileName -Subject "AffinityRules" -SmtpServer "smtp.inc.com"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

2 Replies
Highlighted
User Moderator
User Moderator

Due to a "feature" in Export-Csv it uses the first row of the exported array to determine how many columns there will be in the CSV.

This can be bypassed by sorting the array in such a way that the row with the most members comes first.

$fileName = $env:Temp + '\AffinityRules.csv'

Get-DrsClusterGroup -PipelineVariable group |

ForEach-Object -Process {

    $obj = [ordered]@{

        GroupName = $group.Name

        Cluster = $group.Cluster.Name

    }

    $i = 1

    $group.Member| ForEach-Object -Process {

        $obj.Add("Member$($i)",$_.Name)

        $i++

    }

    New-Object -TypeName PSObject -Property $obj

} | Sort-Object -Property {(Get-Member -InputObject $_ -MemberType NoteProperty).Count} -Descending |

Export-Csv -Path $fileName -NoTypeInformation -UseCulture


Send-MailMessage -From "vca002@inc.com" -To "my_email@inc.com" -Attachments $fileName -Subject "AffinityRules" -SmtpServer "smtp.inc.com"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Highlighted
Enthusiast
Enthusiast

That worked. I wouldn't have been able to come up with this. Thanks again LucD

0 Kudos