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"
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Does this answer your question? [Add objects to an array of objects in Powershell](https://stackoverflow.com/questions/11266013/add-objects-to-an-array-of-objects-in-powershell) – zett42 Mar 25 '21 at 22:23
  • 2
    No @zett42 please don’t encourage that construct. – Doug Maurer Mar 25 '21 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*] – Lee_Dailey Mar 25 '21 at 22:35
  • @Lee_Dailey I see you always giving the path to least resistance :) – Santiago Squarzon Mar 25 '21 at 22:58
  • As an aside: [`Write-Host` is typically the wrong tool to use](http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/), unless the intent is to write _to the display only_, bypassing the success output stream and with it the ability to send output to other commands, capture it in a variable, or redirect it to a file. To output a value, use it _by itself_; e.g., `$value` instead of `Write-Host $value` (or use `Write-Output $value`, though that is rarely needed); see [this answer](https://stackoverflow.com/a/60534138/45375) – mklement0 Mar 25 '21 at 22:59
  • 1
    @SantiagoSquarzon - i am a naturally lazy person ... [*grin*] – Lee_Dailey Mar 25 '21 at 23:46

1 Answers1

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

Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • 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 –  Mar 26 '21 at 01:28
  • 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. – Santiago Squarzon Mar 26 '21 at 01:33
  • look my capture that's my CSV –  Mar 26 '21 at 01:46
  • There, I edited my code so it creates an object with the same column names as your csv. – Santiago Squarzon Mar 26 '21 at 01:50