VMware Cloud Community
AGFlora
Enthusiast
Enthusiast
Jump to solution

error when using the Export-CSV cmdlet

Hi

I get the follwoing error when trying to pipe output to a csv file

" Cannot bind argument to parameter 'InputObject' because it is null. "

The output goes to the csv file but I receive this error at the end of the script.

Here is the code:

##Variables

###################################

$report = @()

$CSVfile = "d:\Vm_Report.csv"

$VMs = get-vmhost | Get-VM

###################################

foreach ($VM in $VMs){

$VMx = Get-View $VM.ID

$HW = $VMx.guest.net

foreach ($dev in $HW)

{

foreach ($ip in $dev.ipaddress)

{

$report += $dev | select @{Name = "Name"; Expression = {$vm.name}},@{Name = "IP"; Expression = {$ip}}, @{Name = "MAC"; Expression = {$dev.macaddress}},

@{Name = "Ownership DL"; Expression = {$vm.CustomFields}}

}

}

}

$report | Export-CSV -NoTypeInformation $CSVfile

Thanks

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The attached script implements the test on the $HW variable.

In my test environment the Export-Csv now works without any problems.

Give it a try.


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

View solution in original post

0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

This normally happens when 1 or more of the rows in the array contain 1 or more fields that are equal to $null.

Just experienced a similar thing this morning Smiley Sad


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

So I guess one would have to live with this as long as the desired results ae achieved. Is there no fix for this?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm not sure you will get all the rows in your CSV. I suspect the Export-Csv cmdlet stops when it encounters the error.

You could add some logic to avoid the passing of $null values.

Perhaps something like this

##Variables
###################################
$report = @()

$CSVfile = "d:\Vm_Report.csv"

$VMs = get-vmhost | Get-VM
###################################

foreach ($VM in $VMs){
	$VMx = Get-View $VM.ID
	$HW = $VMx.guest.net
	foreach ($dev in $HW)
	{
		foreach ($ip in $dev.ipaddress)
		{
			$report += $dev | select @{Name = "Name"; Expression = {if($vm.name -ne $null){$vm.name}else{"null"}}},
									@{Name = "IP"; Expression = {if($ip -ne $null){$ip}else{"null"}}}, 
									@{Name = "MAC"; Expression = {if($dev.macaddress -ne $null){$dev.macaddress}else{"null"}}},
									@{Name = "Ownership DL"; Expression = {if($vm.CustomFields -ne $null){$vm.CustomFields}else{"null"}}}

		}
	}
}

$report | Export-CSV -NoTypeInformation $CSVfile 


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

AGFlora
Enthusiast
Enthusiast
Jump to solution

LucD

As always thanks for you help but unfortunately I still get the error at the scripts end and a whole lot of undesirable data in the CSV file

http://Template Version, 1.0

http://Template Version, 1.0

http://Template Version, 4.0

http://Template Version, 5.0

http://Template Version, 4.0

http://Template Version, 4.0

0 Kudos
LucD
Leadership
Leadership
Jump to solution

One of the problems you're seeing comes from the customfields I suspect.

You seem to have multiple customfields on your guests.

Luckily, the CustomFields property is a hash table, that means you can get at the individual fields by using the name of the field as the key.

The 2nd problem, the error message about the inputObject being null, could come from guests that do not have VMware Tools installed or do not have the tools running.

In that case the $VMx.guest.net property will not even exist and thus the $HW variable will contain $null.

The following version should avoid that problem by adding a test.

And it also show how to get 2 specific CustomFields in the report

$report = @()

$CSVfile = "d:\Vm_Report.csv"

$VMs = Get-VM
###################################

foreach ($VM in $VMs){
	$VMx = Get-View $VM.ID
	if($VMx.Guest.State -eq "NotRunning" -or $VMx.Guest.State -eq "NotInstalled"){
		$HW = $VMx.guest.net
		foreach ($dev in $HW)
		{
			foreach ($ip in $dev.ipaddress)
			{
				$report += $dev | select @{Name = "Name"; Expression = {if($vm.name -ne $null){$vm.name}else{"null"}}},
				@{Name = "IP"; Expression = {if($ip -ne $null){$ip}else{"null"}}}, 
				@{Name = "MAC"; Expression = {if($dev.macaddress -ne $null){$dev.macaddress}else{"null"}}},
				@{Name = "CustFld1"; Expression = {if($vm.CustomFields -ne $null){$vm.CustomFields}else{"null"}}},
				@{Name = "CustFld2"; Expression = {if($vm.CustomFields -ne $null){$vm.CustomFields}else{"null"}}}

			}
		}
	}
}

$report | Export-CSV $CSVfile -NoTypeInformation 

Since there are square brackets involved, I attached the script as well.


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Now there are no errors but the csv file comes up blank.

Do you see anythng wrong with the following code?:

###################################

$report = @()

$CSVfile = "d:\Vm_Report.csv"

$VMs = Get-VM

###################################

foreach ($VM in $VMs){

$VMx = Get-View $VM.ID

if($VMx.Guest.State -eq "NotRunning" -or $VMx.Guest.State -eq "NotInstalled"){

$HW = $VMx.guest.net

foreach ($dev in $HW)

{

foreach ($ip in $dev.ipaddress)

{

$report += $dev | select @{Name = "Name"; Expression = {if($vm.name -ne $null){$vm.name}else{"null"}}},

@{Name = "IP"; Expression = {if($ip -ne $null){$ip}else{"null"}}},

@{Name = "MAC"; Expression = {if($dev.macaddress -ne $null){$dev.macaddress}else{"null"}}},

@{Name = "Ownership DL"; Expression = {if($vm.CustomFields -ne $null){$vm.CustomFields}else{"null"}}}

}

}

}

$report | Export-CSV $CSVfile -NoTypeInformation

#######################################################################################################################

##Disconnect from the virtual center server

Disconnect-VIServer -Confirm:$False

############################################

Write-Host "Vm_Report.csv file created on D:\" -fore Yellow

Invoke-Expression $CSVfile

###########################################

0 Kudos
LucD
Leadership
Leadership
Jump to solution

It seems I screwed up when copying the code.

The test should have been

...
 if(!($VM.Guest.State -eq "NotRunning" -or $VM.Guest.State -eq "NotInstalled")){
...


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Still getting the " Cannot bind argument to parameter 'InputObject' because it is null" error at the end..

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I was able to reproduce your problem by adding some Red Hat 3 guests in the environment.

Apparently for those kind of guests the property $VMx.guest.net can be $null.

You could add a test if the variable $HW equals $null and skip the loop through the network adapters.


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

LucD

Again forgive me for being such a novice. I tried to figure out the code to run a test on the $HW variable by looking at one of the other scripts that you've helped me with but came up empty.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The attached script implements the test on the $HW variable.

In my test environment the Export-Csv now works without any problems.

Give it a try.


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Thanks! It now works without any errors...

0 Kudos