1
需要跨多臺計算機更新.INI文件並更改內容。我有以下的腳本作品:替換或添加.INI文件內容
(Get-Content SDA_Apps.ini) | Foreach-Object {
$_ -replace "UserName=.+", "UserName=Test" `
-replace "UserEmail=.+", "[email protected]" `
-replace "UserNo=.+", "UserNo=1234" `
-replace "UserKey=.+", "UserKey=^%&$*$778-" `
-replace "KEM=.+", "KEM=H10"
} | Set-Content SDA_Apps.ini
有時文本的那些行不存在,我需要添加文本,而不是取代它。
這是我試圖做到這一點 - 沒有成功:
function setConfig($file, $key1, $value1, $key2, $value2) {
$content = Get-Content $file
if ($content -match "^$key\s*=") {
$content $_ -replace "^$key1\s*=.*", "$key1=$value1" -replace "^$key2\s*=.*", "$key2=$value2"|
Set-Content $file
} else {
Add-Content $file "$key1 = $value1"
Add-Content $file "$key2 = $value2"
}
}
setConfig "SDA_Apps.ini" "UserName" "Test" "UserEmail" "[email protected]"
感謝Martin表示完美。 – Ankh2054