VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

issue appending result to csv

Hi,

I am facing issue appending the output to csv file as the second result is over writing the csv file.

please help.

$TotalCPU = 300

$UsedCPU = 200

$cpu = $TotalCPU - $UsedCPU

if (($cpu) -le 0) {

    "vCPU Validation Failed"

    return

}

else {

    "vCPU Validation Success"

$resultsarray = @()

# Create a new custom object to hold our result.

$contactObject = new-object PSObject

# Add our data to $contactObject as attributes using the add-member commandlet

$contactObject | add-member -membertype NoteProperty -name "CPU" -Value $cpu

# Save the current $contactObject by appending it to $resultsArray ( += means append a new element to ‘me’)

$resultsarray += $contactObject

$resultsarray | Export-csv D:\cpu_mem.csv -NoTypeInformation -Append

}

$Totalmem = 250

$Usedmem = 100

$mem = $Totalmem - $Usedmem

if (($mem) -le 0) {

    "Memory Validation Failed"

    return

}

else {

    "Memory Validation Success"

$resultsarray = @()

# Create a new custom object to hold our result.

$contactObject = new-object PSObject

# Add our data to $contactObject as attributes using the add-member commandlet

$contactObject | add-member -membertype NoteProperty -name "Memory" -Value $mem

# Save the current $contactObject by appending it to $resultsArray ( += means append a new element to ‘me’)

$resultsarray += $contactObject

$resultsarray | Export-csv D:\cpu_mem.csv -NoTypeInformation -Append

}

Error

Export-csv : Cannot append CSV content to the following file: D:\cpu_mem.csv. The appended object does not have a

property that corresponds to the following column: CPU. To continue with mismatched properties, add the -Force

parameter, and then retry the command.

At D:\check_host_mem_datastore_freespace.ps1:127 char:18

+ $resultsarray1 | Export-csv D:\cpu_mem.csv -NoTypeInformation -Append

+                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (CPU:String) [Export-Csv], InvalidOperationException

    + FullyQualifiedErrorId : CannotAppendCsvWithMismatchedPropertyNames,Microsoft.PowerShell.Commands.ExportCsvComman

   d

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

In that case, try like this

$TotalCPU = 300

$UsedCPU = 200

$Totalmem = 250

$Usedmem = 100


$resultsarray = @()


$cpu = $TotalCPU - $UsedCPU

$mem = $Totalmem - $Usedmem


$contactObject = New-Object -TypeName PSObject -Property @{

     CPU = 0

     Memory = 0

}


if (($cpu) -le 0) {

     "vCPU Validation Failed"

     return

}

else {

     "vCPU Validation Success"

     $contactObject.CPU = $cpu

}

if (($mem) -le 0) {

     "Memory Validation Failed"

     return

}

else {

     "Memory Validation Success"

     $contactObject.Memory = $mem

}

$resultsarray += $contactObject


$resultsarray | Export-csv D:\cpu_mem.csv -NoTypeInformation -Append


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

You can only append to a CSV that has the same columns as your objects you want to append to it.

Try something like this

$TotalCPU = 300

$UsedCPU = 200

$cpu = $TotalCPU - $UsedCPU

if (($cpu) -le 0) {

     "vCPU Validation Failed"

     return

}

else {

     "vCPU Validation Success"

     $resultsarray = @()

     # Create a new custom object to hold our result.

     $contactObject = New-Object -TypeName PSObject -Property @{

          CPU = $cpu

          Memory = 0

     }

     # Save the current $contactObject by appending it to $resultsArray ( += means append a new element to ‘me’)

     $resultsarray += $contactObject

     $resultsarray | Export-csv D:\cpu_mem.csv -NoTypeInformation -Append

}

$Totalmem = 250

$Usedmem = 100

$mem = $Totalmem - $Usedmem

if (($mem) -le 0) {

     "Memory Validation Failed"

     return

}

else {

     "Memory Validation Success"

     $resultsarray = @()

     $contactObject = New-Object -TypeName PSObject -Property @{

          CPU = 0

          Memory = $mem

     }

     # Save the current $contactObject by appending it to $resultsArray ( += means append a new element to ‘me’)

     $resultsarray += $contactObject

     $resultsarray | Export-csv D:\cpu_mem.csv -NoTypeInformation -Append

}


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

I am getting the output as below

CPU    Memory

100    0

0       150

But I am trying to get the output as below both in the same row

CPU    Memory

100    150

0 Kudos
LucD
Leadership
Leadership
Jump to solution

In that case, try like this

$TotalCPU = 300

$UsedCPU = 200

$Totalmem = 250

$Usedmem = 100


$resultsarray = @()


$cpu = $TotalCPU - $UsedCPU

$mem = $Totalmem - $Usedmem


$contactObject = New-Object -TypeName PSObject -Property @{

     CPU = 0

     Memory = 0

}


if (($cpu) -le 0) {

     "vCPU Validation Failed"

     return

}

else {

     "vCPU Validation Success"

     $contactObject.CPU = $cpu

}

if (($mem) -le 0) {

     "Memory Validation Failed"

     return

}

else {

     "Memory Validation Success"

     $contactObject.Memory = $mem

}

$resultsarray += $contactObject


$resultsarray | Export-csv D:\cpu_mem.csv -NoTypeInformation -Append


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Perfect LucD,

That worked, thank you very much Smiley Happy

0 Kudos