ViCoS
Contributor
Contributor

Export-Csv issue

Jump to solution

Hi,

I'm trying to export networknames for each virtual machine in a cluster but have come to an issue where the output for networkname from Export-Csv is "System.Object[]" rather then the networkname itself.

If I dont use Export-Csv and just output to the screen, it is fine.

I'm using VI Toolkit 1.5. This used to work with version 1.0 so I gather this must have changed? Oddly, sometimes it does display the networkname in v1.5 for some entries.

Here's the script that I'm running (please critique if you can see areas for improvement):

foreach ($cluster in (get-cluster MYCLUSTER| sort -property Name))

{

foreach ($vm in ($cluster | get-vm | sort -property Name))

{

$vminfo = "" | select-object cluster, vm, networkname

$vminfo.cluster = $cluster

$vminfo.vm = $vm

$vminfo.networkname = get-networkadapter -vm $vm | select networkname

$vmcol += $vminfo

}

}

$vmcol | Export-Csv "c:\output.csv" -NoTypeInformation

The output is:

cluster,vm,networkname

MYCLUSTER,MY-VM1,System.Object[]

MYCLUSTER,MY-VM2,System.Object[]

MYCLUSTER,MY-VM3,System.Object[]

MYCLUSTER,MY-VM4,System.Object[]

MYCLUSTER,MY-VM5,System.Object[]

MYCLUSTER,MY-VM6,System.Object[]

Is there anything I'm doing anything wrong?

If I use this instead of the Export-csv line to display the output on screen, it displays the correct data:

$vmcol | select cluster, vm, networkname | ft -auto

thanks,

Chi

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

The Get-NetworkAdapter cmdlet returns a NetworkAdapterImpl object, not just the name.

Also note that a guest can have more than 1 network adapter, in that case the Get-NetworkAdapter cmdlet returns an array of NetworkAdapterImpl objects.

This should do the trick.

$vmcol = @()
foreach ($cluster in (get-cluster | sort -property Name))
{


	foreach ($vm in ($cluster | get-vm | sort -property Name))
{
		foreach($network in (Get-NetworkAdapter -VM $vm)){
			$vminfo = "" | select-object cluster, vm, networkname
			$vminfo.cluster = $cluster
			$vminfo.vm = $vm
			$vminfo.networkname = $network.networkname
			$vmcol += $vminfo
		}
	}
}


$vmcol | Export-Csv "c:\output.csv" -NoTypeInformation 


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership

The Get-NetworkAdapter cmdlet returns a NetworkAdapterImpl object, not just the name.

Also note that a guest can have more than 1 network adapter, in that case the Get-NetworkAdapter cmdlet returns an array of NetworkAdapterImpl objects.

This should do the trick.

$vmcol = @()
foreach ($cluster in (get-cluster | sort -property Name))
{


	foreach ($vm in ($cluster | get-vm | sort -property Name))
{
		foreach($network in (Get-NetworkAdapter -VM $vm)){
			$vminfo = "" | select-object cluster, vm, networkname
			$vminfo.cluster = $cluster
			$vminfo.vm = $vm
			$vminfo.networkname = $network.networkname
			$vmcol += $vminfo
		}
	}
}


$vmcol | Export-Csv "c:\output.csv" -NoTypeInformation 


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

View solution in original post

0 Kudos
ViCoS
Contributor
Contributor

looks as if they have changed they way it works.

Before, it would display my networknames in curly braces like: {mynet1, mynet2}. Using your method it works in a different way, but I'm happy that I get the correct info out. Thanks!

0 Kudos