简体   繁体   中英

How to replace third position of assembly version in the PowerShell script

I have powershell script to replace assembly versions, but i have to change the version number at third postion LIKE [assembly: AssemblyVersion("1.0.20.1")] to [assembly: AssemblyVersion("1.0.21.1")]

This is what I have, which increments last postion:

#
# 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)

I would personally make an object out of it with values for the various parts, then you can increment whichever part you want and restructure it into a string later.

$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]
    }
}

Then you can increment as simply as $Version.Third++ to increase the third set. Then just use a formatted string to spit it back out:

'AssemblyVersion("{0}.{1}.{2}.{3}")' -f $Version.First, $Version.Second, $Version.Third, $Version.Fourth

That will produce AssemblyVersion("1.0.21.1") just like you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM