Skip to content

Instantly share code, notes, and snippets.

@shiranGinige
Created September 21, 2012 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save shiranGinige/3760640 to your computer and use it in GitHub Desktop.
Save shiranGinige/3760640 to your computer and use it in GitHub Desktop.
Powershell script to changes references of all projects under the execution directory
# 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