question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

az functionapp config appsettings set Errors When Used in PowerShell with Multiple Settings

See original GitHub issue

Describe the bug

I have been trying to troubleshoot why I am unable to use the documented --settings switch with multiple space separated Key=Value pairs Documentation in a PowerShell script I have for provisioning. In my production script I end up having all of the secrets roll into the first secret which I am trying to reproduce.

I have created a small test script to try to troubleshoot the issue and was able to get a slightly different error message.

Command Name az functionapp config appsettings set

Errors:

The command failed with an unexpected error. Here is the traceback:
not enough values to unpack (expected 2, got 1)
Traceback (most recent call last):
  File "D:\a\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/util.py", line 557, in shell_safe_json_parse
  File "json\__init__.py", line 359, in loads
  File "json\decoder.py", line 337, in decode
  File "json\decoder.py", line 355, in raw_decode
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\a\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/command_modules/appservice/custom.py", line 392, in update_app_settings
  File "D:\a\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/util.py", line 583, in shell_safe_json_parse
azure.cli.core.azclierror.InvalidArgumentValueError: Failed to parse string as JSON:
at
Error detail: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\a\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\knack/cli.py", line 233, in invoke
  File "D:\a\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/__init__.py", line 663, in execute
  File "D:\a\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/__init__.py", line 726, in _run_jobs_serially
  File "D:\a\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/__init__.py", line 718, in _run_job
  File "D:\a\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/command_modules/appservice/commands.py", line 40, in _ex_handler
  File "D:\a\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/__init__.py", line 697, in _run_job
  File "D:\a\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/__init__.py", line 333, in __call__
  File "D:\a\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/command_operation.py", line 121, in handler
  File "D:\a\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/command_modules/appservice/custom.py", line 403, in update_app_settings
ValueError: not enough values to unpack (expected 2, got 1)

To Reproduce:

Steps to reproduce the behavior. Note that argument values have been redacted, as they may contain sensitive information.

  • az functionapp config appsettings set --name {} --resource-group {} --settings {} {} {} {} {} {} {}

The command is being generated from this PowerShell Script (I have redacted any private information):

$appSettingsToUpdate = [System.Collections.Generic.Dictionary[string, string]]::new()
$appSettingsToUpdate.Add("Setting1", "Set at $(Get-Date)")
$appSettingsToUpdate.Add("Setting2", "Set at $(Get-Date)")

[System.Text.StringBuilder]$settingsString = [System.Text.StringBuilder]::new()
foreach ($appSettingToUpdate in $appSettingsToUpdate.GetEnumerator()) {
    $settingsString.Append("""$($appSettingToUpdate.Key)=$($appSettingToUpdate.Value)"" ") | Out-Null
}

$expandedSettingsString = $settingsString.ToString()

# Set the new settings; This will restart the function app
az functionapp config appsettings set --name {REDACTED} --resource-group {REDACTED} --settings $expandedSettingsString | Out-Null

Expected Behavior

I would expect this to work. This should be equivalent to:

az functionapp config appsettings set --name {REDACTED} --resource-group {REDACTED} --settings "Setting1=Set at 09/16/2022 09:21:42" "Setting2=Set at 09/16/2022 09:21:42"

Which does work when manually run through the command line

Workaround

I suspect that there is probably some weird behavior with PowerShell and the way that this parses commands. I am going to attempt a work around by just providing the settings as a JSON file. However this is not documented to work this way for functionapp but is documented to work this way for webapp (See Documentation) I suspect that this will work even if not documented to do so (that should be fixed).

Environment Summary

Windows-10-10.0.19044-SP0
Python 3.10.5
Installer: MSI

azure-cli 2.40.0

Extensions:
azure-devops 0.25.0

Dependencies:
msal 1.18.0b1
azure-mgmt-resource 21.1.0b1

Additional Context

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
aolszowkacommented, Sep 19, 2022

@jiasli BINGO! Thank you for linking the PowerShell issue that is what I was looking for as I felt the original code met the requirements of a space separated “Key=Value” pair. It is PowerShell’s mishandling of the double quotes (in versions prior to 7.3) that was the root cause of this issue.

The intent of this line:

$settingsString.Append("""$($appSettingToUpdate.Key)=$($appSettingToUpdate.Value)"" ") | Out-Null

Was to produce a string that looked like "Setting1=Set at 2022/09/19" "Setting2=Set at 2022/09/19" however as per the linked PowerShell issue this was munged. It is helpful to know that the Azure CLI provides this output:

cli.knack.cli: Command arguments: ['functionapp', 'config', 'appsettings', 'set', '--name', 'REDACTED', '--resource-group', 'REDACTED', '--settings', 'Setting1=Set at 09/19/2022 11:28:21', 'Setting2=Set at 09/19/2022 11:28:21', '--debug']

0reactions
jiaslicommented, Sep 19, 2022

The fact that $expandedSettingsString gets torn apart is due to PowerShell issue https://github.com/PowerShell/PowerShell/issues/1995:

> $expandedSettingsString = """a=b c"" ""d=e f"""
> $expandedSettingsString
"a=b c" "d=e f"
> python -c "import sys; print(sys.argv)" $expandedSettingsString
['-c', 'a=b', 'c d=e', 'f']
> python -c "import sys; print(sys.argv)" '"a=b c" "d=e f"'
['-c', 'a=b', 'c d=e', 'f']

I guess it’s because when calling native exe, PowerShell passes $expandedSettingsString as

""a=b c" "d=e f""

This is fixed in PowerShell 7.3:

> python -c "import sys; print(sys.argv)" $expandedSettingsString
['-c', '"a=b c" "d=e f"']
> python -c "import sys; print(sys.argv)" '"a=b c" "d=e f"'
['-c', '"a=b c" "d=e f"']

The correct way to pass multiple arguments is to use

> $expandedSettingsString="a=b c", "d=e f"
> python -c "import sys; print(sys.argv)" @expandedSettingsString
['-c', 'a=b c', 'd=e f']
> python -c "import sys; print(sys.argv)" "a=b c" "d=e f"
['-c', 'a=b c', 'd=e f']
Read more comments on GitHub >

github_iconTop Results From Across the Web

az functionapp config appsettings set Munges values into first ...
az functionapp config appsettings set Munges values into first setting when no spaces are used in PowerShell with multiple settings #23920.
Read more >
Issue with ) when using Azure CLI to set Azure Function app ...
1 Answer 1 · Add additional double quotes for force powershell to treat the argument as a literal: az functionapp config appsettings set...
Read more >
az functionapp config appsettings - Microsoft Learn
Set using both key-value pair and a json file with more settings. Azure CLI Copy. Try It.
Read more >
Update-AzFunctionAppSetting (Az.Functions) | Microsoft Learn
This command adds a new app setting in a function app. Parameters. -AppSetting. Hashtable with keys and values describe the app settings to...
Read more >
App settings reference for Azure Functions | Microsoft Learn
App settings in a function app contain configuration options that affect all functions for that function app. When you run locally, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found