Skip to content

Instantly share code, notes, and snippets.

@alanmbarr
Forked from shiranGinige/ChangeAllReferences.ps1
Last active December 12, 2017 19:55
Show Gist options
  • Save alanmbarr/6a25f40c4bd70a8d7f8e20d8be0b3508 to your computer and use it in GitHub Desktop.
Save alanmbarr/6a25f40c4bd70a8d7f8e20d8be0b3508 to your computer and use it in GitHub Desktop.
Powershell script to changes references of all projects under the execution directory
#ReplacePackage -ReferenceToRemove $encompasspkg -VersionNumber $versionSDK
function ReplacePackage(){
param([String]$ReferenceToReplace, [String]$VersionNumber)
$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:PackageReference[@Include='{0}']", $ReferenceToReplace)
[System.Console]::WriteLine("Finding reference {0} in {1}", $ReferenceToReplace, $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}", $ReferenceToReplace , $projectPath)
#Removing the child
$parentNode = $node.ParentNode;
$parentNode.RemoveChild($node);
#Adding a new item
$referenceNode = $proj.CreateElement("PackageReference", $xmlns);
$referenceNode.SetAttribute("Include", $ReferenceToReplace);
$parentNode.AppendChild($referenceNode)
$Version = $proj.CreateElement("Version", $xmlns);
$Version.InnerXml = $VersionNumber;
$referenceNode.AppendChild($Version);
$proj.Save($projectPath)
}
else
{
[System.Console]::WriteLine("Cannot find the assembly reference {0} in project {1}", $ReferenceToReplace , $projectPath)
}
}
}
@alanmbarr
Copy link
Author

This is a script I forked to update package references in the csproj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment