V
V
Vellis-msk2020-05-31 10:57:17
PowerShell
Vellis-msk, 2020-05-31 10:57:17

Is it possible to replace variables with PowerShell?

Good afternoon.
There is a file, let's call it params.ini, with the following content:
{param: variable1} = sample1
{param: variable2} = sample2
{param: variable3} = sample3
There is a Test directory, with nested directories and files.
The task is to get the values ​​​​of variables from the params.ini file using the PowerShell script and replace them in all found files of all directories in the Test folder by mask, suppose in all *.sql, *.json, *conf files.
Help me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaxKozlov, 2020-05-31
@Vellis-msk

Something like

$params [email protected]{}
Get-Content d:\params.ini | foreach-object {
If $_ -match '\[param:(^]+)\]\s*=\s*(.*)' { $params[$matches[1]}=$matches[2] }
}
$filelist = get-childitem d:\work *json,*sql
Foreach $file in$filelist {
Get-content $file | foreach-object {
If $_ -match 'Тут нужный regex для файла' -and $params.contains($matches[1]){
$_=$_ - replace 'тут тоже регекс', $params[$atches[1])
}
} |set-content $file
}

The syntax was not verified, something will have to be corrected of course

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question