現在、テキスト ファイルの 1 行の編集に取り組んでいます。テキスト ファイルを上書きしようとすると、テキスト ファイルに 1 行しか返されません。で関数を呼び出そうとしています
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
「check=0」がありません。
私は何を逃したのですか?