Search code examples
powershellazure-devopsmsbuild-task

Use special character in custom VSTS PowerShell script task


I am currently facing the problem that I would like to set the Copyright of my AssemblyInfo dynamically during the VSTS build to something like this:

© 2012-2018 Company Name

but the 2018 should be set dynamically representing the current year. Therefor I tried to set a global build variable to something like this:

© 2012-$(Date:yyyy) Company Name

Here the copyright special character works fine but the date does not work at this place so the next thing I tried was to make a custom PowerShell script build task with an inline script. I set again my global variable ("Copyright") this time to

-set during build-

and in the inline script I tried to replace the value like this:

$date=$(Get-Date -Format yyyy);
$_Copyright = "© 2012-$date Company Name";
Write-Host "##vso[task.setvariable variable=Copyright]$_Copyright";

Now I got my dynamic date working but on my shipped dlls I get "c 2012-2018 Company Name" with a 'c' instead of the '©'.

So I replaced it with [char]0x00A9:

$date=$(Get-Date -Format yyyy);
$_Copyright = [char]0x00A9 + " 2012-$date Company Name";
Write-Host "##vso[task.setvariable variable=Copyright]$_Copyright";

But nothing changes in the result: "c 2012-2018 Company Name"

Although on my local machine my PowerShell gives me for [char]0x00A9 the '©' sign.

Any suggestions?


Solution

  • I can reproduce this issue.

    Seems the logging command cannot recognize the symbol ©. So we cannot set the variable which including the © as the value by below command:

    Write-Host "##vso[task.setvariable variable=Copyright]$_Copyright"

    In your scenario you can try below workarounds:

    1, Using Copyright (c) instead of Copyright ©, the logging command can recognize the string (c):

    $date=$(Get-Date -Format yyyy);
    $_Copyright = "(c) 2012-$date Company Name";
    Write-Host "##vso[task.setvariable variable=Copyright]$_Copyright";
    

    2, Combine variables:

    1) Set a global build variable e.g.: $(Symbol) and set © as the value

    2) Set a date variable using logging command :

    $date=$(Get-Date -Format yyyy);
    $Company= "2012-$date Company Name";
    Write-Host "##vso[task.setvariable variable=Company]$Company";
    

    3) Use the variable $(Symbol)$(Company) together to get the entire Copyright string.

    Alternately you can directly use ©$(Company) to check if that works.