at first we should handle the error "op_addition"
op_addition is a method used by array vars.
you declare
$data = 0
and after you did
$data+=$VM.UsedSpaceGB
So Powershell, return an error! He only does what you ask him to do
SO the import-csv problem, working with a csv file should be done always by import-csv, convertfrom-csv is to use a variable array as a csv. like a http get or so..
i correct your script in this way.
$csvreport = @()
$i=0
$VMNum=0
$data=0
$VMs=Get-VM
$VMCount=$VMs.count
$ofile="C:\Temp\DataGrowth.csv"
# Check for file / Read old file if exists
if (Test-Path $ofile){
$csvreport = Import-Csv $ofile -UseCulture
}
foreach($VM in $VMs) {
#Get-Vm | Foreach-object {
$entity = $VM
$VMNum++
#Progress Bar
$Activity="Collecting Stats for VM"
Write-Progress -Activity "Collecting stats for VM $($entity.Name)" -Status "VM $VMNum of $VMCount" -PercentComplete (($i/$VMCount)*100)
$data = $data + $VM.UsedSpaceGB
$i++
}
# Creates List of Custer Details
$clusterdetails = "" |select-object Collection_Date,Total_Size,VM_Count
$clusterdetails.Collection_Date=Get-Date -format d
$clusterdetails.Total_Size=[math]::round(($data), 5)
$clusterdetails.VM_Count=$VMCount
$csvreport += $clusterdetails
$csvreport | export-csv -NoTypeinformation $ofile -UseCulture
Remove-Variable $csvreport