0

I have this PowerShell script:

Import-Module ActiveDirectory

Get-ADUser -filter 'SN -eq "Buyl"' -Properties UserPrincipalName,EmployeeNumber,Description,Department,GivenName,sn |
select @{N='UPN';E={$_.UserPrincipalName}},@{N='POINTER';E={$_.EmployeeNumber}},@{N='DESCRIPTION';E={$_.Description}},@{N='DEPARTMENT';E={$_.Department}},@{N='LASTNAME';E={$_.GivenName}},@{N='FIRSTNAME';E={$_.sn}},@{N='OU';E={$ou}} |
ConvertTo-Json

This works fine in PowerShell with users like Anaïs in their first name. I run this script from a PHP script that parses the Json output using json_decode. It seems the special characters break this function.

How can I make sure the PowerShell sends the output in clean UTF8 format that can be parsed with PHP?

  • Either [convert the output stream in PHP](https://stackoverflow.com/questions/47942172/how-to-convert-output-of-windows-shell-exec-in-php-to-utf8) or update `[System.Console]::OutputEncoding` on the PowerShell side before executing – Mathias R. Jessen Aug 26 '20 at 20:49
  • As aside, shouldn't `@{N='LASTNAME';E={$_.GivenName}}` be `@{N='FIRSTNAME';E={$_.GivenName}}` and `@{N='FIRSTNAME';E={$_.sn}}` be `@{N=LASTNAME';E={$_.sn}}`? – Theo Aug 27 '20 at 11:46
  • Changing the [System.Console]::OutputEncoding to UTF8 is working. Thanks for that. And indeed, I mixed up two fields. – Fredmatrack Aug 28 '20 at 20:26

0 Answers0