Forum Discussion
stop expanding parentheses in a variable
Hello
We have created a script using PowerShell and Box CLI to create groups in Box as a user request them. We have had success with the script until recently.
We had a user request that failed
$ClientName="Test-Group(Name)"
$BoxClientOwnerName="$($ClientName)_Owner"
$BoxLOWGRPRaw="$( Box groups:create --json $BoxClientOwnerName )"
The script returns the error
_Owner was unexpected at this time.
If we enter the group name not as a variable, it will work
$BoxLOWGRPRaw="$( Box groups:create --json "Test-Group(Name)" )"
We believe that it is trying to expand the information in the ()
We have tried several different ways to create the group variable name, but have not had success yet.
We have tried to use escape characters to stop the processing of the (), but no success.
Could you please help us resolve this issue?
Thank you
2 Replies
Hi,
PowerShell is not expanding the parentheses — the issue is that your variable is passed without quotes, so Box CLI receives two separate tokens:
Test-Group(Name) _OwnerThis is why you get:
_Owner was unexpected at this timeTo fix it, you must preserve the quotes around the argument when calling Box:
$ClientName = "Test-Group(Name)"; $BoxClientOwnerName = "$ClientName`_Owner"; $BoxLOWGRPRaw = $( Box groups:create --json "`"$BoxClientOwnerName`"" );Or more cleanly:
powershell
$BoxLOWGRPRaw = Box groups:create --json $BoxClientOwnerName | ConvertFrom-Json;(Here PowerShell handles quoting correctly.)
The parentheses are not the problem — the missing quotes are.
- LainRobertsonSilver Contributor
Hi Jhult1170 ,
I do not have a command called "box", so I can't test your exact use case, however, it's worth noting that your manual example line of:
$BoxLOWGRPRaw="$( Box groups:create --json "Test-Group(Name)" )"Is not what your first three lines would be producing. Rather, based on your initial three lines, the proper manual test case would look like:
$BoxLOWGRPRaw = "$( Box groups:create --json Test-Group(Name)_Owner )"This is because you have used the $BoxClientOwnerName variable in your third line rather than the $ClientName variable.
Cheers,
Lain