0

I want to add a object to my array $computers but it said that the index is out of bounds

function ajouterperipherique ($id, $emplacement, $type) { 
      $computers = import-csv -path  "C:\Temp\Peripherique.csv"
      echo $computers


  $newObject = [pscustomobject]@{
      idObject = $id
      EmplacementObject = $emplacement
      TypeObject=$type
  }


  for ($i = 0; $i -lt $computers.Count; $i++) {
    if ($i +1 -eq $computers.count) {
      $computers[$computers.count+1]=$newObject
      
    }
  }
  

 Write-Host ($newObject | Format-List | Out-String)
 
}

ajouterperipherique "GLADIATOR" "ordinateur" "Statique"
6
  • Does this answer your question? Add objects to an array of objects in Powershell Commented Mar 25, 2021 at 22:23
  • 2
    No @zett42 please don’t encourage that construct. Commented Mar 25, 2021 at 22:28
  • 1
    if all you want to do is ADD one item to an existing CSV file ... and you KNOW the header ... then just make a [PSCustomObject] with the needed info and use Export-CSV with the -Append parameter. that is what the parameter is FOR ... [grin] Commented Mar 25, 2021 at 22:35
  • @Lee_Dailey I see you always giving the path to least resistance :) Commented Mar 25, 2021 at 22:58
  • 1
    @SantiagoSquarzon - i am a naturally lazy person ... [grin] Commented Mar 25, 2021 at 23:46

1 Answer 1

1

Here is the solution proposed by Lee_Dailey:

$csvPath='C:\Temp\Peripherique.csv'

function ajouterperipherique {
param(
    [string]$ID,
    [string]$Emplacement,
    [string]$Type,
    [string]$Path
)

    if(-not(Test-Path $Path) -or -not [IO.Path]::GetExtension($Path) -eq '.csv')
    {
        throw 'File doest not exist or is not a Csv...'
    }

    [pscustomobject]@{
        Identifiant = $id
        Type = $type
        Emplacement = $emplacement
    }|Export-Csv $Path -NoTypeInformation -Append
}

ajouterperipherique -ID "GLADIATOR" -Emplacement "ordinateur" -Type "Statique" -Path $csvPath

A few tips, as pointed out in comments, you shouldn't really use or you should try to avoid Write-Host whenever possible.

You shouldn't really hardcode paths inside your functions, since they're meant to be re-used, hardcoding information you know can change in the future is never a good idea.

You might also wanna consider setting your parameters as Mandatory, parameters are somewhat important in Powershell and can make your life easier. I recommend reading this article if you're thinking of creating more functions in the future: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.1

Sign up to request clarification or add additional context in comments.

4 Comments

I have a bug :Export-Csv: Unable to add CSV content to the following file: C: \ Temp \ Peripherique.csv. The added object does not have a property corresponding to the following column: Identifiant;type;Emplacement;. To continue with different properties, add the setting
Well, the error is pretty much self-explanatory. It means your already existing Csv doesn't have the same headers as the ones you're trying to add.
look my capture that's my CSV
There, I edited my code so it creates an object with the same column names as your csv.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.