2017-02-08 1 views
1

У меня есть файл, который имеет переменнуюполучить значение переменной из файла и добавления новой

$versionNumber = "1.0.0 

мне нужно изменить значение переменной для "$versionNumber = "user_choice" "явно.

#------------------------------------------------ 
# Pack parameters used to create the .nupkg file. 
#------------------------------------------------ 

# Specify the Version Number to use for the NuGet package. If not specified, the version number of the assembly being packed will be used. 
# NuGet version number guidance: https://docs.nuget.org/docs/reference/versioning and the Semantic Versioning spec: http://semver.org/ 
# e.g. "" (use assembly's version), "1.2.3" (stable version), "1.2.3-alpha" (prerelease version). 
$versionNumber = "1.0.0" 

# Specify any Release Notes for this package. 
# These will only be included in the package if you have a .nuspec file for the project in the same directory as the project file. 
$releaseNotes = "" 

# Specify a specific Configuration and/or Platform to only create a NuGet package when building the project with this Configuration and/or Platform. 
# e.g. $configuration = "Release" 
#  $platform = "AnyCPU" 
$configuration = "" 
$platform = "" 

Любой возможный подход рекомендуется

ответ

0

Используйте командлет Get-Content, чтобы прочитать файл, найти переменную versionNumber с помощью положительного регулярного выражения lookbehind и заменить его. Set-Content Командлет записать его обратно:

(Get-Content 'youFile.nupkg' -raw) -replace '(?<=\$versionNumber\s=).+', '"user_choice"' | 
    Set-Content 'youFile.nupkg' 
+0

Я использую этот скрипт в Jenkins и вызова user_choice в качестве параметра. это работает, но новое значение, что я вижу, это $ VersionNumber = 1.0.0 но фактическое значение должно заменить be $ versionNumber = "1.0.0". Здесь не хватает места и "". (Get-Content "C: \ TestFolder \ Config.ps1" -raw) -replace '(? <= \ $ VersionNumber \ s =). +', "$ Env: VERSION" | Set-Content "C: \ TestFolder \ Config.ps1" – Kally

0

другое решение без регулярных выражений

(get-content "C:\temp\nuckpkg.txt").Replace('$releaseNotes = ""', '$releaseNotes = "user_choice"') | 
    set-content "C:\temp\nuckpkg.txt" 
0

Вы не правильно использовать код, предложенный Мартином.

вы используете "$env:Version", которые наконец-то истолкованы к значению переменного только ... «"»является частью замены синтаксиса.

вы должны использовать его таким образом "'$env:Version'".

С уважением,

Kvprasoon