1

I want to change the value in TypeAdressage column in my csv if the identifiant is found when it loop but i dont know how i can do it i try .getTypeAdressage but it's doesn't work

The CSV:

Identifiant;type;Emplacement;TypeAdressage;rack;fabriquant;dateachat
srv4Q5;serveur;Q3506;Statique;R3342;;
PWIN10;ordinateur;Q3507;Statique;;DELL;2020-01-01
Q00032;ordinateur;Q3507;Statique;;DELL;2020-01-01
Q00033;ordinateur;Q3507;Statique;;LENOVO;2021-01-01
Q00033;ordinateur;Q3507;Statique;;DELL;2020-02-03
function Changer-Type {
    param(
        [string]$Identifiant,
        [string]$TypeAdressage,
        [string]$Path
    )

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

    $computers = Import-Csv -Path $Path -Delimiter ';'

    $computers | Where-Object { $_.Identifiant -eq $Identifiant } | ForEach-Object { $_.TypeAdressage = $TypeAdressage }
    #write the updated $computers object array back to disk
    $computers | Export-Csv -Path $Path -NoTypeInformation -Delimiter ';' -Force
}

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

Changer-Type -Identifiant "Q00032" -TypeAdressage "Dynamique" -Path $csvPath
8
  • PowerShell isn't Java ;-) drop the get prefix: $computers[$i].Identifiant -eq $Identifiant Commented Mar 26, 2021 at 16:08
  • I drop the get but it doesn't change the value of collumn TypeAdressage add index of $i in my CSV file and I don't know why Commented Mar 26, 2021 at 16:16
  • Are you sure the CSV has those columns? What does Import-Csv "C:\Temp\Peripherique.csv" |Get-Member show? Commented Mar 26, 2021 at 16:25
  • i just remove it of the csv files :) Commented Mar 26, 2021 at 16:29
  • You never write anything back to the file, so I'm not sure how you're determining whether it works or not? What happens if you add a $Computers |Export-Csv C:\temp\output.csv to the end of the function - do you get the expected output in that file then? Commented Mar 26, 2021 at 16:31

1 Answer 1

2

As Mathias R. Jessen already commented, you never save anything back to disk..

To find rows where the Identifiant is equal to what you send to the function, use a Where-Object clause, because looking at the image, there can be multiple rows with the same Identifiant value..

Try

function Changer-Type {
    param(
        [string]$Identifiant,
        [string]$TypeAdressage,
        [string]$Path
    )

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

    $computers = Import-Csv -Path $Path -Delimiter ';'

    $computers | Where-Object { $_.Identifiant -eq $Identifiant } | 
                 ForEach-Object { $_.TypeAdressage = $TypeAdressage }
    # write the updated $computers object array back to disk
    $computers | Export-Csv -Path $Path -Delimiter ';'  -NoTypeInformation
}

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

Changer-Type -Identifiant "Q00032" -TypeAdressage "Dynamique" -Path $csvPath

P.S. I have changed the function name to comply with the PowerShell Verd-Noun convention

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

14 Comments

I do but it doesn't change the value on TypeAdressage in the output csv file
in my tests id does. What character is used as delimiter in the file? Is that not a comma? In that case use parameter -Delimiter
Thanks Theo, but it doesn't change the value in my CSV check the code if it is ok. code's link: zupimages.net/viewer.php?id=21/12/94mz.png CSV image link : zupimages.net/viewer.php?id=21/12/wtzx.png
You are using a semicolon delimited file. Change to $computers = Import-Csv -Path $Path -Delimiter ';' to read the file, and $computers | Export-Csv -Path $Path -NoTypeInformation -Delimiter ';' to write to the file.
thanks, but it doesn't affect Q00032 TyepAdressage to "Dynamique"
|