如何替换PowerShell脚本中程序集版本的第三个位置

如何替换PowerShell脚本中程序集版本的第三个位置,powershell,.net-assembly,Powershell,.net Assembly,我有powershell脚本来替换程序集版本,但我必须在第三个位置更改版本号 类似于[assembly:AssemblyVersion(“1.0.20.1”)]到 [组件:组件版本(“1.0.21.1”)] 这就是我所拥有的,它增加了最后一个职位: # # This script will increment the build number in an AssemblyInfo.cs file # $assemblyInfoPath = "C:\Users\kondas\Deskt

我有powershell脚本来替换程序集版本,但我必须在第三个位置更改版本号 类似于[assembly:AssemblyVersion(“1.0.20.1”)]到 [组件:组件版本(“1.0.21.1”)]

这就是我所拥有的,它增加了最后一个职位:

#
# This script will increment the build number in an AssemblyInfo.cs file
#

$assemblyInfoPath = "C:\Users\kondas\Desktop\PowerShell\AssemblyInfo.cs"

$contents = [System.IO.File]::ReadAllText($assemblyInfoPath)

$versionString = [RegEx]::Match($contents,"(AssemblyFileVersion\("")(?:\d+\.\d+\.\d+\.\d+)(""\))")
Write-Host ("AssemblyFileVersion: " +$versionString)

#Parse out the current build number from the AssemblyFileVersion
$currentBuild = [RegEx]::Match($versionString,"(\.)(\d+)(""\))").Groups[2]
Write-Host ("Current Build: " + $currentBuild.Value)

#Increment the build number
$newBuild= [int]$currentBuild.Value +  1
Write-Host ("New Build: " + $newBuild)

#update AssemblyFileVersion and AssemblyVersion, then write to file


Write-Host ("Setting version in assembly info file ")
$contents = [RegEx]::Replace($contents, "(AssemblyVersion\(""\d+\.\d+\.\d+\.)(?:\d+)(""\))", ("`${1}" + $newBuild.ToString() + "`${2}"))
$contents = [RegEx]::Replace($contents, "(AssemblyFileVersion\(""\d+\.\d+\.\d+\.)(?:\d+)(""\))", ("`${1}" + $newBuild.ToString() + "`${2}"))
[System.IO.File]::WriteAllText($assemblyInfoPath, $contents)

我个人会用不同部分的值制作一个对象,然后你可以增加任何你想要的部分,然后重新构造成一个字符串

$Version = $contents | ?{$_ -match 'AssemblyVersion\("(\d+)\.(\d+)\.\(d+)\.(\d+)"\)'}|%{
    [PSCustomObject]@{
        [int]'First'=$Matches[1]
        [int]'Second'=$Matches[2]
        [int]'Third'=$Matches[3]
        [int]'Fourth'=$Matches[4]
    }
}
然后您可以简单地增加
$Version.Third++
以增加第三组。然后只需使用格式化字符串将其吐出:

'AssemblyVersion("{0}.{1}.{2}.{3}")' -f $Version.First, $Version.Second, $Version.Third, $Version.Fourth
这将产生您想要的
AssemblyVersion(“1.0.21.1”)