VMware Cloud Community
uswbnc53
Enthusiast
Enthusiast

Export to export-xls function or export-csv

I am wanting to use the export-xls function or export-csv cmdlet to export the output of a script to a spreadsheet. However, I am having issues figuring out how to format the script for use with the function. I know part of my problem is that my output is a string. When I attempt to redirect to export-csv the output is a column of numbers.

$Num=1.5
$Date = get-date -format g

Get-VMHost | ForEach-Object {
$GetVMHost= $_

$DhcpEnabled = $GetVMHost | Get-VMHostNetworkAdapter -VMKernel | % { $_.DhcpEnabled }

if ($DhcpEnabled -eq "")
       { "$Date,$GetVMHost,$Num,DHCP = $DhcpEnabled,Compliant" }
else   { "$Date,$GetVMHost,$Num,DHCP = $DhcpEnabled,Not Compliant"}

}

I want to format to be something like this.

Date, Hostname, Number, Value, Status

2/11/2012 7:02 PM,hosstname.domain,1.5,DHCP = False False,Compliant
2/11/2012 7:02 PM,hosstname.domain,1.5,DHCP = False False,Compliant
2/11/2012 7:02 PM,hosstname.domain,1.5,DHCP = False False,Compliant

Tags (2)
0 Kudos
5 Replies
LucD
Leadership
Leadership

Instead of producing strings as result, produce objects.

They are easier to manipulate and convert to a CSV file.

Something like this for example

$Num=1.5
$Date = get-date -format g
$report = Get-VMHost | ForEach-Object {
    $GetVMHost= $_
   
$DhcpEnabled = $GetVMHost | Get-VMHostNetworkAdapter -VMKernel | % { $_.DhcpEnabled }     if ($DhcpEnabled -eq ""){         $text = "Compliant"
    }     else{         $text = "Not Compliant"
    }     New-Object PSObject -Property @{         Date = $Date
        Host
= $GetVMHost
        Num
= $Num
        DHCP
= $DhcpEnabled
        Compliant
= $text
    } } $report | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture


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

0 Kudos
uswbnc53
Enthusiast
Enthusiast

Hi Luc.

I really appreciate your reply! That worked pretty well.

Except, when the export occurs to the csv the output for the DHCP column isn't the value expected. It exports as System.Object[].

I noticed it was returning a value for both NICs attached to the management network and I fixed that hoping that was the issue, but it is still exporting as seen below.

DHCPHostCompliantDateNum
System.Object[]hostname.domainCompliant2/11/2012 21:561.5
System.Object[]hostname.domainCompliant2/11/2012 21:561.5
System.Object[]hostname.domainCompliant2/11/2012 21:561.5
System.Object[]hostname.domainCompliant2/11/2012 21:561.5
System.Object[]hostname.domainCompliant2/11/2012 21:561.5
System.Object[]hostname.domainCompliant2/11/2012 21:561.5

If I run $report without the export-csv I get the below output as expected.

DHCP      : False
Host      : hostname.domain
Compliant : Compliant
Date      : 2/12/2012 1:29 PM
Num       : 1.5

DHCP      : False
Host      : hostname.domain
Compliant : Compliant
Date      : 2/12/2012 1:29 PM
Num       : 1.5

0 Kudos
LucD
Leadership
Leadership

My mistake, the $DhcpEnabled variable is an array and Export-Csv handles those as you saw in the output.

One solution is to put all array elements, seperated by a comma, in a string.

$Num=1.5
$Date = get-date -format g 
$report
= Get-VMHost | ForEach-Object {     $GetVMHost= $_
   
$DhcpEnabled = $GetVMHost | Get-VMHostNetworkAdapter -VMKernel | % { $_.DhcpEnabled }     if ($DhcpEnabled -eq ""){         $text = "Compliant"
    }     else{         $text = "Not Compliant"
    }     New-Object PSObject -Property @{         Date = $Date
        Host = $GetVMHost
        Num = $Num         DHCP = [string]::Join(',',$DhcpEnabled)         Compliant = $text
    } }
$report | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture


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

0 Kudos
uswbnc53
Enthusiast
Enthusiast

That worked!

Just one more question, if you don't mind.

Why do the columns not match the order in the script when they are exported? I tried to re-arrange the values in the script and they still exported to the csv the same.

Date = $Date
Host = $GetVMHost
Num = $Num
Value = [string]::Join(',',$DhcpEnabled)
Compliant = $text

ValueHostCompliantDateNum
FALSEhostname.domainCompliant2/12/2012 14:351.5
FALSEhostname.domainNot Compliant2/12/2012 14:351.5
FALSEhostname.domainCompliant2/12/2012 14:351.5
0 Kudos
LucD
Leadership
Leadership

The New-Object cmdlet doesn't garantue the order.

To have the properties in a specific order in the CSV file, you will have to throw in a Select-Object cmdlet before the export.

$report | Select Date,Host,Num,DHCP,Compliant |
Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture 

In PowerShell v3 this will change, there you specify an order of the properties on the New-Object cmdlet.


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

0 Kudos