从文件中获取变量值并附加new

时间:2017-02-08 05:09:07

标签: powershell powershell-v2.0 powershell-v3.0

我有一个有变量的文件

 $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 = ""

建议采取任何可行的方法。

3 个答案:

答案 0 :(得分:0)

使用Get-Content cmdlet读取文件,使用正向lookbehind正则表达式查找versionNumber变量并替换它。最后,使用Set-Content cmdlet将其写回:

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

答案 1 :(得分:0)

没有正则表达式的其他解决方案

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

答案 2 :(得分:0)

您没有正确使用Martin建议的代码。

你正在使用"$env:Version",它最终会被解释为变量值......'''是替换语法的一部分。

你应该以这种方式使用"'$env:Version'"

此致

Kvprasoon