This is the first part of a larger script that I am building. I need to work out an output error before moving forward. I'm using PowerCLI to execute esxcli commands on hosts in specific clusters. The script completes but I receive only the following output in the CSV fields linked with the esxcli commands.
VMware.VimAutomation.ViCore.Cmdlets.Commands.EsxCli.EsxCliMethodElement
Any suggestions on how to properly format the esxcli commands?
$report = @()
$EsxHosts = Get-VMHost -Location "Host Staging"
foreach($EsxHost in $EsxHosts){
$esxcli = Get-EsxCli -VMHost $EsxHost -V2
$line = "" | Select Host,AL,PL
$line.Host = $EsxHost.Name
$line.AL = $esxcli.system.account.list
$line.PL = $esxcli.system.permission.list
$report += $line
}
$report | Export-Csv -Path "C:\scripts\vmware\PCI.csv" -NoTypeInformation -UseCulture
Thank you!
Yes, that is what tried to explain earlier, the commands return an array of lines.
And since Export-Csv can't handle arrays, it tries to explain that by showing the type of the object it sees, i.e. System.Object[]
Where the '[]' indicates it is seeing an array.
There are a couple of options to export the output.
- you can 'join' all the lines together, so the array becomes a single string
- create a row per entry in the array, but since you are trying to export 2 such arrays, that will produce a lot rows with a lot of redundant data
How do you envisage seeing the results in a CSV?
What do you want see in each row?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
When you use the V2 switch, you have to use the Invoke() method on each call.
$line.AL = $esxcli.system.account.list.Invoke()
$line.PL = $esxcli.system.permission.list.Invoke()
be aware that both calls return multi-lines results.
Not sure if that is what you want in your CSV
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for the quick reply! I had used the invoke method and it returns the following in the CSV.
System.Object[]
Any ideas on why the results don't properly export?
Once I get the proper data to export I'll work on cleaning it up.
Thanks
Yes, that is what tried to explain earlier, the commands return an array of lines.
And since Export-Csv can't handle arrays, it tries to explain that by showing the type of the object it sees, i.e. System.Object[]
Where the '[]' indicates it is seeing an array.
There are a couple of options to export the output.
- you can 'join' all the lines together, so the array becomes a single string
- create a row per entry in the array, but since you are trying to export 2 such arrays, that will produce a lot rows with a lot of redundant data
How do you envisage seeing the results in a CSV?
What do you want see in each row?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I really just need the data. Creating a single string should be acceptable.
Thanks,
You could do something like this
$line.AL = ($esxcli.system.account.list.Invoke()) -join '|'
$line.PL = ($esxcli.system.permission.list.Invoke()) -join '|'
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I think we are getting closer. These lines:
$line.AL = ($esxcli.system.account.list.Invoke()) -join '|'
$line.PL = ($esxcli.system.permission.list.Invoke()) -join '|'
Instead of the list data the following is returned in the CSV:
VMware.VimAutomation.ViCore.Impl.V1.EsxCli.EsxCliObjectImpl|VMware.VimAutomation.ViCore.Impl.V1.EsxCli.EsxCliObjectImpl|VMware.VimAutomation.ViCore.Impl.V1.EsxCli.EsxCliObjectImpl|VMware.VimAutomation.ViCore.Impl.V1.EsxCli.EsxCliObjectImpl
Thanks for your continued help on this.
After a little more research I figured out a method to export the data in a usable format. I used the following.
$line.AL = ($esxcli.system.account.list.Invoke() | Out-String).Trim()
$line.PL = ($esxcli.system.permission.list.Invoke() | Out-String).Trim()
Thanks!