Powershell - Piped input to GC CLI

I'm trying to run a command to add users to a group using powershell. Im getting an error when i pipe the JSON payload in, but it works when i use the -f option.

Is the piped input command specific? I have been able to pipe in JSON with other commands

for example, running this command will create a search based on my JSON:
'{"query":[{"value": "GROUPNAME","fields":["name"],"type":"EXACT"}]}' | gc.exe groups search create

but when i try the same command to add a user to a group, i get an error:

'{"memberIds":["UID"],"version": 0}' | gc.exe groups members add GROUPID

gc.exe : invalid character 'ï' looking for beginning of value
At line:1 char:73

  • ... ion": 0}' | gc.exe groups members add GROUPID...
  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (invalid charact...inning of value:String) [], RemoteException
    • FullyQualifiedErrorId : NativeCommandError

Ok, i found the problem. Powershell was formatting my request in a character encoding that GC CLI didnt like.

Changed the way Powershell handles encoding using the following:

$OutputEncoding = [console]::OutputEncoding

now when it gets to my section of code that was failing, it works.

Hey @Josh_L_Davis I have this issue all the time with powershell when using some tools (probably whenever tools are built/designed for Unix-like platforms).

I've tried different things like what you had mentioned but still face issues when piping output like you mentioned too (although some tools work like jq)

May I ask what your [Console]::OutputEncoding & [Console]::InputEncoding is set to that made it work in your case?

Both my in/out encodings are set to utf-8/65001 (EncodingName/CodePage) but thought I'd ask

Alright well I also figured out my issue as well, this seems to be a pretty reasonable powershell default when working with problematic external tools:

[System.Console]::InputEncoding = [System.Console]::OutputEncoding = $OutputEncoding = [System.Text.UTF8Encoding]::new($false)

This sets the console input/output encoding as well as the special variable for $OutputEncoding to utf-8.

The last bit, [System.Text.UTF8Encoding]::new($false), was crucial to getting things working for me. The $false here corresponds to the encoderShouldEmitUTF8Identifier parameter of that constructor which for reasons I don't understand, allows me to pass raw text content thru the pipeline to executables that expect normal text input, as opposed to setting those 3 configurables to just [System.Text.Encoding]::UTF8

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.