我目前正在编辑文本文件的一行。当我尝试覆盖文本文件时,我只能在文本文件中返回一行。我正在尝试使用
modifyconfig "test" "100"
config.txt:
检查=0 测试=1
modifyConfig()功能:
Function modifyConfig ([string]$key, [int]$value){
    $path = "D:\RenameScript\config.txt"
    ((Get-Content $path) | ForEach-Object {
        Write-Host $_
        # If '=' is found, check key
        if ($_.Contains("=")){
            # If key matches, replace old value with new value and break out of loop
            $pos = $_.IndexOf("=")
            $checkKey = $_.Substring(0, $pos)
            if ($checkKey -eq $key){
                $oldValue = $_.Substring($pos+1)
                Write-Host 'Key: ' $checkKey
                Write-Host 'Old Value: ' $oldValue
                $_.replace($oldValue,$value)
                Write-Host "Result:" $_
            }
        } else {
            # Do nothing
        }
    }) | Set-Content ($path)
}
我收到的结果config.txt:
测试=100
我错过了“检查= 0”。
我错过了什么?