0

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")]

1 Answer 1

1

You perform a match with:

$copyrightInData = ($Data -match $copyrightSearchTxt)

That just returns an array with 1 item: the one line in the file that matches your pattern. Then you run:

$Data = ($Data -replace $copyrightInData, $newCopyright)

That looks at the whole file, looks for something that matches the pattern defined in $copywriteInData, and replaces it with the new copywrite text. Here's the thing, this is a Regular Expression (RegEx for short) match. So $copywriteInData is the string [assembly: AssemblyCopyright("Copyright © Company 2014 - 2018")]. In RegEx anything within square brackets means that it should match any character within, so when you do the -replace it looks at the first character and decides:

Does it match: A
Does it match: s
Does it match: s
Does it match: e
Does it match: m

It proceeds until it finds a character within the [ ] that it matches, or runs out of characters within the [ ] to match against. How to fix this is to properly escape your string for RegEx matching:

$Data = $Data -replace [regex]::Escape($copyrightInData), $newCopyright

A simpler way to do this would be to better define what you want to replace, then just read the file, perform the replace, and write the file.

    $currentYear         = (date).Year
    $copyrightSearchTxt  = '(?<=\[assembly: AssemblyCopyright\(").*?(?="\)])'
    (GC $copyrightFileWithPath) -replace $copyrightSearchTxt, $currentYear | Set-Content $copyrightFileWithPath
Sign up to request clarification or add additional context in comments.

2 Comments

Wow thank you for the explanation! As well as an alternate path! It works beautifully now. I really like how you're able to pull the replace text with $copyrightSearchTxt = '(?<=[assembly: AssemblyCopyright(").*?(?=")])' instead of having to search the text from the beginning. Really clean.
Regular Expressions can be very powerful when used correctly (and I'm decent, but not a pro compared to some around this site). I used what are called a Look Behind and a Look Ahead to get that match. If you are interested in learning more about RegEx I strongly suggest checking out regular-expressions.info

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.