JoshLDavis | 2023-03-13 19:30:01 UTC | #1
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
JoshLDavis | 2023-03-13 21:21:56 UTC | #2
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.
evdev | 2023-04-12 19:28:30 UTC | #3
Hey @JoshLDavis 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
evdev | 2023-04-12 19:49:57 UTC | #4
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
system | 2023-05-12 19:50:00 UTC | #5
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
This post was migrated from the old Developer Forum.
ref: 18869