loader-logo

Hi,
Have you ever been stuck with some kind of boring work, While trying to update Assembly Versions for your .net projects? It must be hectic if you have a whole bunch of projects within your solution, right?

And the most interesting part is that, as we programmers are already busy dealing with logic and finding different ways to solve the problem, we don’t even have time to scratch our heads. So in this whole bunch of tensions, we have to update the assembly versions while upgrading the solution for new changes.

I was been in the same situation where I did some changes for the client and I have to go through all projects and find it’s AssemblyInfo.cs and update version number to 2.0.1.0.

I was searching a way I can automatically update all assembly versions of projects within my solution and at that time I never knew the Power of PowerShell. I came across PowerShell scripts, I ignore that many times. But after several googling, I came to the script again. Because I have to look into that once before I go to the whole manual process.

Below is the code:

function Usage 
echo "This is ";
{
echo “Usage: “;
echo ” from cmd.exe: “;
echo ” powershell.exe SetVersion.ps1 2.8.3.0″;
echo ” “;
echo ” from powershell.exe prompt: “;
echo ” .\SetVersion.ps1 2.8.3.0″;
echo ” “;
}

function Update-SourceVersion
{
Param ([string]$Version)
$NewVersion = ‘AssemblyVersion(“‘ + $Version + ‘”)’;
$NewFileVersion = ‘AssemblyFileVersion(“‘ + $Version + ‘”)’;
foreach ($o in $input)
{
Write-output $o.FullName
$TmpFile = $o.FullName + “.tmp”
get-content $o.FullName |
%{$_ -replace ‘AssemblyVersion\(“[0-9]+(\.([0-9]+|\*)){1,3}”\)’, $NewVersion } |
%{$_ -replace ‘AssemblyFileVersion\(“[0-9]+(\.([0-9]+|\*)){1,3}”\)’, $NewFileVersion } > $TmpFile
move-item $TmpFile $o.FullName -force
}
}
function Update-AllAssemblyInfoFiles ( $version )
{
foreach ($file in “AssemblyInfo.cs”, “AssemblyInfo.vb” )
{
get-childitem -recurse |? {$_.Name -eq $file} | Update-SourceVersion $version ;
}
}
# validate arguments
$r= [System.Text.RegularExpressions.Regex]::Match($args[0], “^[0-9]+(\.[0-9]+){1,3}$”);
if ($r.Success)
{
Update-AllAssemblyInfoFiles $args[0];
}
else
{
echo ” “;
echo “Bad Input!”
echo ” “;
Usage ;
}

source

Copy-Paste the above code into the file and save that with extension ps1

Now Place the file into the root of your solution. Now run your Window’s PowerShell As an Administrator

Once you do that you will a blue screen something like this:

Now redirect to open your path in PowerShell prompt. while tying cd.. *with your path

Type dir to make sure you are on the right solution folder and have script file as well.

 .\Update_Script.ps1

Is the command you will type and hit enter to run that script.

Once you did, and Boom…!! WHAT THE HECK IS THAT?

.\Update_Script : File E:\SampleProject\Update_Script.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .\Update_Script
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

Hey Hey KEEP CALM! This is just a policy exception, you just have to allow this script to run on this computer Because PowerShell is very powerful so window plays safe with every script(and you must have too).

Just run this to allow windows to run this script.

Set-ExecutionPolicy RemoteSigned

Once you did that now you will be allowed to run your script. Just type the below code in the PowerShell window along with the version you want to set for your projects within the solution. Like:

.\Update_Script 3.0.0

Once you did that You will see that all your assembly versions are now upgraded to 3.0.0.

That’s all folks!

That’s how Smart dev works 😉

Stay tuned for more perks.
Happy Coding 🙂

0 Points