VMware Cloud Community
SteveR77
Contributor
Contributor
Jump to solution

Export-Csv Not generating expected output


I am attempting to generate a report from the NIC Teaming script but given the following code will I have to create and Excel object and define it's elements?

The Export-Csv option is not working as I expect it to in this case. Thank you for your assistance.  The code follows:

($VMHost in Get-VMHost){

Foreach ($vSwitch in ($VMHost |Get-VirtualSwitch )){

$NicTeaming = Get-NicTeamingPolicy -VirtualSwitch $vSwitch

$obj = new-object psobject

$obj |Add-Member -membertype NoteProperty -Name Host -value $VMHost

$obj |Add-Member -membertype NoteProperty -Name vSwitch -value $vSwitch

$obj |Add-Member -membertype NoteProperty -Name NumPorts -value $vSwitch.NumPorts

$obj |Add-Member -membertype NoteProperty -Name NumPortsAvailable -value $vSwitch.NumPortsAvailable

$PG = $vSwitch |Get-VirtualPortGroup

If ($PG.Count-gt 1){

$obj |Add-Member -membertype NoteProperty -Name PortGroups -value ([string]::join(‘ ,‘,($PG)))

}

Else {

$obj | Add-Member -membertype NoteProperty -Name PortGroups -value $PG

}

$obj |Add-Member -membertype NoteProperty -Name BeaconInterval -value $NicTeaming.BeaconInterval

$obj |Add-Member -membertype NoteProperty -Name LoadBalancingPolicy -value $NicTeaming.LoadBalancingPolicy

$obj |Add-Member -membertype NoteProperty -Name NetworkFailoverDetectionPolicy -value $NicTeaming.NetworkFailoverDetectionPolicy

$obj |Add-Member -membertype NoteProperty -Name NotifySwitches -value $NicTeaming.NotifySwitches

$obj |Add-Member -membertype NoteProperty -Name FailbackEnabled -value $NicTeaming.FailbackEnabled


If ($NicTeaming.ActiveNic-gt 1){

$obj |Add-Member -membertype NoteProperty -Name ActiveNic -value ([string]::join(‘ ,‘,($NicTeaming.ActiveNic)))

}

Else {

$obj |Add-Member -membertype NoteProperty -Name ActiveNic -value $NicTeaming.ActiveNic

}

If ($NicTeaming.StandbyNic-gt 1){

$obj |Add-Member -membertype NoteProperty -Name StandbyNic -value ([string]::join(‘ ,‘,($NicTeaming.StandbyNic)))

}

Else {

$obj |Add-Member -membertype NoteProperty -Name StandbyNic -value $NicTeaming.StandbyNic

}

If ($NicTeaming.UnusedNic-gt 1){

$obj |Add-Member -membertype NoteProperty -Name UnusedNic -value ([string]::join(‘ ,‘,($NicTeaming.UnusedNic)))

}

Else {

$obj |Add-Member -membertype NoteProperty -Name UnusedNic -value $NicTeaming.UnusedNic

}

$obj |Add-Member -membertype NoteProperty -Name CheckBeacon -value $NicTeaming.CheckBeacon

$obj

$Report += $obj

}

$Report = $Report | Sort-Object VMName

IF ($Report -ne "")

{ $Report | Export-Csv NIC-Teaming.csv }

}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Did you declare $report as an array ?

On the first line put

$report = @()


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

View solution in original post

0 Kudos
10 Replies
LucD
Leadership
Leadership
Jump to solution

I suspect that is because you are trying to export an array with rows that do not have the same layout.

All the rows in the array should have the same properties.

Any error messages ?


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

SteveR77
Contributor
Contributor
Jump to solution

LucD,

No error message.  The code executes but the expected CSV report is never generated.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you declare $report as an array ?

On the first line put

$report = @()


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

0 Kudos
SteveR77
Contributor
Contributor
Jump to solution

LucD,

Thank you very much sir.  That was it.

As an aside when is your next book on VM performance due out?

Thank you again and make it a great day.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm targeting 2014Q2.


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

0 Kudos
DJ_dshivamu
Enthusiast
Enthusiast
Jump to solution

Hello Lucd

I am getting following error message when i execute, can you please help me?

Unexpected token 'in' in expression or statement.

At D:\script\switch.ps1:3 char:12

+ ($VMHost in <<<<  Get-VMHost){

    + CategoryInfo          : ParserError: (in:String) [], ParseException

    + FullyQualifiedErrorId : UnexpectedToken

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I suspect there might be a typo in the preceding lines somewhere.

Can you eventually share the script you are using (as a file attachment) ?


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

0 Kudos
DJ_dshivamu
Enthusiast
Enthusiast
Jump to solution

Hello Lucd

Thanks For response, Please find script, let me know what changes went wrong 😉

0 Kudos
LucD
Leadership
Leadership
Jump to solution

This line

($VMHost in Get-VMHost){

should be changed into

foreach($VMHost in Get-VMHost){


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

0 Kudos
DJ_dshivamu
Enthusiast
Enthusiast
Jump to solution

excellent Luc,, its working now 🙂 thanks for your help

0 Kudos