Fairly new to Powershell and have been working to replace outdated copyrights within an assembly file with updated copyrights. Using the function below however fills the entire Data with a repeat of my newCopyright text.
What am I doing wrong?
function UpdateCopyright{
param
(
[Parameter(Mandatory=$True,Position=1)]
[String] $copyrightFileWithPath = "_"
)
try
{
if ($copyrightFileWithPath -ne "_"){
$currentYear = (date).Year
$copyrightNewTxt = "Copyright © Company $currentYear"
$copyrightSearchTxt = "assembly: AssemblyCopyright"
$newCopyright = "[assembly: AssemblyCopyright(""$copyrightNewTxt"")]"
$Data = (Get-Content $copyrightFileWithPath)
$copyrightInData = ($Data -match $copyrightSearchTxt)
$Data = ($Data -replace $copyrightInData, $newCopyright)
Set-Content $copyrightFileWithPath $Data
return $true
}
else{
Write-Host ("ERROR: Invalid parameter to modify copyright for file " + $copyrightFileWithPath)
return $false
}
}
catch
{
$ErrorMessage = $_.Exception.Message
Write-Host ("ERROR: Exception while modifying copyright for file " + $copyrightFileWithPath + $ErrorMessage)
return $false
}
}
Sample Input file:
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[assembly: AssemblyProduct("Test App")]
[assembly: AssemblyCopyright("Copyright © Company 2014 - 2018")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("2.11.0.4")]
[assembly: AssemblyFileVersion("2.11.0.4")]
Sample Output file:
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[assembly: AssemblyProduct("Test App")]
[assembly: AssemblyCopyright("Copyright © Company 2018")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("2.11.0.4")]
[assembly: AssemblyFileVersion("2.11.0.4")]