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
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
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
So I guess one would have to live with this as long as the desired results ae achieved. Is there no fix for this?
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
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 |
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
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
###########################################
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
Still getting the " Cannot bind argument to parameter 'InputObject' because it is null" error at the end..
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
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.
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
Thanks! It now works without any errors...