Created
September 21, 2012 09:40
-
-
Save shiranGinige/3760640 to your computer and use it in GitHub Desktop.
Powershell script to changes references of all projects under the execution directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Calling Convention | |
# ChangeReference.ps1 "log4net , Version=1.2.0.0 ......" "new dll path" , "log4net , <new dll infor>" | |
# Note : This needs script signing before executing. refer http://www.hanselman.com/blog/SigningPowerShellScripts.aspx | |
# OR can set the powershell execution policy to unrestricted (at your own risk ;)) > Set-ExecutionPolicy unrestricted | |
param([String]$ReferenceToRemove , [String]$NewAssemblyPath, [String]$NewAssemblyIncludeInfo) | |
$xmlns = "http://schemas.microsoft.com/developer/msbuild/2003" | |
$projects = ls -r -i *.csproj | |
foreach($projectPath in $projects) | |
{ | |
$proj = [xml](Get-Content $projectPath) | |
[System.Console]::WriteLine($project); | |
$XPath = [string]::Format("//a:Reference[@Include='{0}']", $ReferenceToRemove) | |
[System.Console]::WriteLine("Finding reference {0} in {1}", $ReferenceToRemove, $projectPath); | |
[System.Xml.XmlNamespaceManager] $nsmgr = $proj.NameTable | |
$nsmgr.AddNamespace('a','http://schemas.microsoft.com/developer/msbuild/2003') | |
$node = $proj.SelectSingleNode($XPath, $nsmgr) | |
if ($node) | |
{ | |
[System.Console]::WriteLine("Removing the assembly reference {0} in project {1}", $ReferenceToRemove , $projectPath) | |
#Removing the child | |
$parentNode = $node.ParentNode; | |
$parentNode.RemoveChild($node); | |
#Adding a new item | |
$referenceNode = $proj.CreateElement("Reference", $xmlns); | |
$referenceNode.SetAttribute("Include", $NewAssemblyIncludeInfo); | |
$parentNode.AppendChild($referenceNode) | |
$hintPath = $proj.CreateElement("HintPath", $xmlns); | |
$hintPath.InnerXml = $NewAssemblyPath; | |
$referenceNode.AppendChild($hintPath); | |
$proj.Save($projectPath) | |
} | |
else | |
{ | |
[System.Console]::WriteLine("Cannot find the assembly reference {0} in project {1}", $ReferenceToRemove , $projectPath) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment