Если такой файл выглядит примерно так:
export KARATE_MYSQL_USER=KarateUser
export KARATE_MYSQL_PASS=Karate@89272363
export VEHICLE_API_PASS=Test@4239
Тогда вам нужно будет выполнить его разбор, как показано ниже:
$envFile = 'X:\variables.env'
$content = Get-Content -Path $envFile -Raw # read the file as single string
$regex = [regex] '(?im)export\s+(?<varname>[^=]+)\s*=\s*(?<value>.+)$'
$match = $regex.Match($content)
while ($match.Success) {
$varName = $match.Groups['varname'].Value
$varValue = $match.Groups['value'].Value
Write-Host "Setting Environment variable '$varName' to '$varValue'"
# set the environment variable in Windows.
# If this does needs to apply to ALL users, use 'Machine' instead of 'User'
[System.Environment]::SetEnvironmentVariable($varName, $varValue, 'User')
$match = $match.NextMatch()
}
Детали регулярного выражения:
export Match the characters “export” literally
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
(? Match the regular expression below and capture its match into backreference with name “varname”
[^=] Match any character that is NOT a “=”
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
= Match the character “=” literally
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(? Match the regular expression below and capture its match into backreference with name “value”
. Match any single character that is not a line break character
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
$ Assert position at the end of a line (at the end of the string or before a line break character)
(?im)
делает регулярное выражение без учета регистра и позволяет ^
и $
совпадать при переносе строки