Problems with encoding in powershell

Hi,

I'm running a powershell script that goes in the Active Directory and creat AD groups. Everything works fine when executed from a developper pc, but when executed from the powershell within TeamCity, it fails. After investigation, we found out, that it was because TeamCity was unable to process the path "Unités-Administratives" because of the french character "é" (It was transform in some form of "@&" something). I remember a post on this forum that show informations on this but can't find it anymore and i would like to apply the correct patch to solve this issue. Furthermore, i remember reading that TeamCity 7.0 had corrected this and now will take care of these characters. Can somebody confirm this for TeamCity 7.0 and link me to the post please.

1 comment
Comment actions Permalink

We have solve this problem on our own. I was quite a pain and a lot of sweat was involve, but it's working now.

This is what we did.
First you have to be sure that your agent is running the good encoding language (cp1252 for french). To do that, click on the agent to go to it's setting page, and click on the agent Parameters tab. Look at the File.encoding it should be the encoding you are trying to use. (I have a lot of rights in Teamcity but i believe you need some special rights to do that. Contact your admin if you can't get this agent information). Once you have verified that everything is fine, you will need to change the configuration of your build to use a file instead of the source code.

In your build, press the edit configuration settings, press the "Build Step: powershell" button on the right and select your powershell script. Inside your build step, there is the script option, instead of picking Source Code take File. Copy paste your powershell source code into a ps1 File.

Now a short parenthesis about your powershell script, because there is some modification that will be needed if you want your script to run smoothly in teamcity. If you have a param() section in your script that is getting build parameter in Teamcity, You will have to remove them and put them in the Script arguments sction of your build step. Example:

My script contains originally this

Add-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue


param (   
[string]$cheminComplet = "%dossier%%fichier%",
[string]$Environnement = "%EnvironnementAD%"
)


"### Récupération des contextes de l'AD : " + $Environnement + ".local"
[Reflection.Assembly]::LoadWithPartialName("Interop.AZROLESLib.dll") | Out-null


$AzStore = new-object -com AzRoles.AzAuthorizationStore
$AzStore.Initialize(0,"MSLDAP://$Environnement.local/CN=azman,CN=Microsoft,CN=Program Data,DC=$Environnement,DC=local",$null)


After modification, my file contains this

Add-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue
Add-PSSnapin Interop.AZROLESLib -ErrorAction SilentlyContinue


$cheminComplet = $args[0]
$Environnement =$args[1]

$AzStore = new-object -com AzRoles.AzAuthorizationStore
$AzStore.Initialize(0,"MSLDAP://"+ $Environnement +".local/CN=azman,CN=Microsoft,CN=Program Data,DC="+ $Environnement+",DC=local",$null)

You can see that i also have change the Reflection line for a new Snapin to keep things clear. It changes nothing to the script execution, it's just to keep one way of doing imports instead of two. The most important modification is the replacement of the param section with 2 global variables that receive the informations needed from the variable $args. $args is a built-in array in powershell that contains all the arguments pass to the script in command line. The order of your arguments in teamcity determine the order where you retrieve them with the index.

Once you have done these minor script adjustment go back to TeamCity. In "Script File" put the name of your file, be aware of the comment just under the textbox about the path relative to checkout directory.

In script execution mode select the value "Execute .ps1 script with "-File" argument"
In script argument put between double quotes and in the right order your argument from the param section deleted above. In my example it would be:

"%dossier%%fichier%" "%EnvironnementAD%"

Take note that between the two arguments, there is a whitespace (that's how you seperate one argument from another in powershell).

After you have done this, you will be able to run your build with the script. If this script contain any special characters supported by the encoding of the agent, it will work.

P.S.: Make sure the agent that run your build is the one with the right encoding

0

Please sign in to leave a comment.