2

I have to rename a bulk of shared mailboxes to a new format, the script is ready and works but when the script runs, the names that contains accentuation are not being updated properly and a question mark is shown where the accentuation should exist.

-----------------------------------------input file:

name,NewName
BMS-cipatubarao,"CIPA Tubarão"
BEL-liberacaeodelimbo-amd,"Liberação AMD"

-----------------------------------------Script:

$sharedChange = Import-Csv .\"SHAREDchange.csv"
$sharedChange | foreach{
  $sharedName = $_.name
  $sharedNewName = $_.newname
  try {
  # Change the DisplayName on Services Domain
      set-user $sharedname -displayname $sharedNewName
      "$(get-date -f MM/dd/yyyy-HH:mm:ss), $sharednewname, modified on, $sharedname`n" | out-file '.\SharedSuccess.txt' -append
  }   catch {
      "$(get-date -f MM/dd/yyyy-HH:mm:ss), $sharednewname, NOT modified on, $sharedname`n" | out-file '.\SharedFailed.txt' -append
  }
}

-----------------------------------------output file:

09/04/2017-17:47:12, CIPA Tubar�o, modified on, cipatubarao
09/04/2017-17:47:14, Libera��o AMD, modified on, liberacaeo-amd

What should I do? I have already tried to use the command directly on my shell using set-mailbox liberacaeo-amd -displayname "Liberação AMD" and it works, but as I have a batch of shared mailboxes to execute this change, this is completely out of question.

2

At first you really should now stop using such special characters as you have seen it will be a problem when it comes to scripting ... so as you start implementing a new name schema make sure this will eliminate such ugly special characters.

1.) Make sure your input format has the correct encoding (e.g. UTF-8 with BOM)

2.) You can try to convert them:

   function ConvertTo-Encoding ([string]$From, [string]$To){
        Begin{
            $encFrom = [System.Text.Encoding]::GetEncoding($from)
            $encTo = [System.Text.Encoding]::GetEncoding($to)
        }
        Process{
            $bytes = $encTo.GetBytes($_)
            $bytes = [System.Text.Encoding]::Convert($encFrom, $encTo, $bytes)
            $encTo.GetString($bytes)
        }
    }

    [System.Text.Encoding]::Default.Codepage

    $Title = "Liberação Tubarão" | ConvertTo-Encoding "UTF-8" "windows-1251"
    Write-Host $Title
    $Title = $Title.Replace(("Liberação" | ConvertTo-Encoding "UTF-8" "windows-1251"), "")
    Write-Host $Title

3.) You might try to set the encoding to UTF8 at first via (more infos here):

[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

Powershell 3.0 and higher:

$PSDefaultParameterValues['*:Encoding'] = 'utf8'
  • 1
    Hi dear BastianW, the problem is that this "ugly characters" are part of the Brazilian Portuguese language, there is no way to remove it as without some words doesnt make sense. I will need to keep them, and that is why I've requested this help. But thanks anyway. – Guilherme Lima Sep 5 '17 at 10:59
  • I know that from German, where they have "äöü" but there are ways to replace them, so that the meaning would be the same "ä=ae;ö=oe;ü=ue", so something like that might work in your case as well. Not every software can "handle" special (UTF-8) characters and soon or less you might run into some issues which are caused by the "special names". This is by the way the reason why Microsoft do not allow them in OUs and such objects as seen here. – BastianW Sep 5 '17 at 11:09
  • Yeah, I understand what you are saying, but, when I do the same command using the shell directly, the name is correctly executed. – Guilherme Lima Sep 5 '17 at 11:10
  • yes as mentioned above this is caused by the encoding. You might try to convert that or to set the correct encording (Nr3) to fix this issue. – BastianW Sep 5 '17 at 11:15
  • 1
    Hm, but this was mentioned as solution 1 in my answer ;-) – BastianW Sep 5 '17 at 11:43
1

thanks for the support. I've done some tests and found out that during importation the names were coming wrong, so, I've saved the input file as .csv UNICODE UTF-8. Did the tests again and everything is fine now. Didn't know about that, but it is fixed now.

Thanks!

more info: http://blogs.catapultsystems.com/thernandez/archive/2015/09/20/how-to-export-and-import-foreign-characters-with-powershell/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.