4 Replies Latest reply on Oct 18, 2019 1:47 AM by ganapa2000

    issue appending result to csv

    ganapa2000 Hot Shot

      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

        • 1. Re: issue appending result to csv
          LucD Guru
          Community WarriorsvExpertUser Moderators

          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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: issue appending result to csv
            ganapa2000 Hot Shot

            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

            • 3. Re: issue appending result to csv
              LucD Guru
              Community WarriorsvExpertUser Moderators

              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: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 4. Re: issue appending result to csv
                ganapa2000 Hot Shot

                Perfect LucD,

                 

                That worked, thank you very much