VMware Cloud Community
nealda
Contributor
Contributor
Jump to solution

How to extract pipelined objects in a ForEach-Object loop

Hi All.

I have a one-liner that extracts the failure results of an all-host Host Profile compliance test. It looks like:

Test-VMHostProfileCompliance -Profile 'My Profile' -UseCache | Select-Object VMHost,VMHostProfile,IncomplianceElementList | Sort-Object VMHost | fl *

It returns good results in a list format:

VMHost : ESXi24
VMHostProfile : My Profile
IncomplianceElementList : {option["key-vim-profile-host-OptionProfile-Net_DVFilterBindIpAddress"]-optionFixed:Option Net.DVFilterBindIpAddress doesn't match the specified criteria,
option["key-vim-profile-host-OptionProfile-Security_AccountLockFailures"]-optionFixed:Option Security.AccountLockFailures doesn't match the specified criteria,
option["key-vim-profile-host-OptionProfile-Syslog_global_logHost"]-optionFixed:Option Syslog.global.logHost doesn't match the specified criteria,
security_SecurityProfile_SecurityConfigProfile.security_UserAccountProfile_UserAccountProfile-ProfExpression:User suppaduppa not present on host.}

(only one host shown in example)

I'd like to get the output into CSV format with one compliance finding in each row but the closest that I can get is just a list of the failures with no context:

Test-VMHostProfileCompliance -Profile 'My Profile' -UseCache | ForEach-Object {$_.IncomplianceElementList.Description}

Is there a way that I can get the VMHost and VMHostProfile names from the pipeline inside the ForEach-Object loop and then export it to a three-column CSV?

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could do something like this

 

Test-VMHostProfileCompliance -Profile 'My Profile' -UseCache -PipelineVariable test | 
ForEach-Object { 
    $_.IncomplianceElementList |
    select @{N='VMHost';E={$test.VMHost.Name}},
        @{N='VMHostProfile';E={$test.VMHostProfile.Name}},
        Description
} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture

 


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

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

You could do something like this

 

Test-VMHostProfileCompliance -Profile 'My Profile' -UseCache -PipelineVariable test | 
ForEach-Object { 
    $_.IncomplianceElementList |
    select @{N='VMHost';E={$test.VMHost.Name}},
        @{N='VMHostProfile';E={$test.VMHostProfile.Name}},
        Description
} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture

 


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

nealda
Contributor
Contributor
Jump to solution

Perfect Perfect Perfect!

Thanks, LucD!

Reply
0 Kudos