0

I have the following function:

function Add-Variable {
    Param
    (
        [Parameter(Mandatory=$true, Position=0)]
        [string] $ProjectName,

        [Parameter(Mandatory=$true, Position=1)]
        [string] $VariableGroupId,

        [Parameter(Mandatory=$true, Position=2)]
        [string] $VariableName,

        [Parameter(Mandatory=$true, Position=3)]
        [string] $Value,

        [Parameter(Mandatory=$false, Position=4)]
        [bool] $IsSecret = $false

    )

    Write-Host
    Write-Host "Adding $($VariableName) variable..."
    $DeploymentPath = az pipelines variable-group variable create --project $ProjectName --group-id $VariableGroupId --name $VariableName --value $Value | Null-Check $VariableName
}

I have multiple calls to this function e.g.

Add-Variable $ProjectAlias $GlobalVarGroup.id 'Deployment.Path' 'D:\Websites\$(Hostname)'
Add-Variable $ProjectAlias $GlobalVarGroup.id 'Apppool.Username' $IISUser

however, when i make the following call:

Add-Variable $ProjectAlias $GlobalVarGroup.id 'Log.Path' '\\svr-prdfs\$(ASPNETCORE_ENVIRONMENT)\Logs\$(Hostname)\$(Agent.MachineName)'

I get this error:

az : \Logs\$(Hostname)\$(Agent.MachineName) was unexpected at this time.
At C:\Users\richa\OneDrive\Documents\Azure CLI\helpers.ps1:24 char:23
+ ... ymentPath = az pipelines variable-group variable create --project $Pr ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (\Logs\$(Hostnam...d at this time.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

I wonder if this is due to the string being passed containing values such as $(Agent.MachineName), however, I am passing these in single quotes so I would expect them to be evaluated as a literal string.

Further to this, the above call works where 'D:\Websites\$(Hostname)' is passed as a parameter, so this would seem to contradict this theory.

Does anyone know what the issue is here?

Here is an example of what I am trying to achieve - this is what I have entered manually into azure devops and am looking to do this via azure cli. enter image description here

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
DrollDread
  • 321
  • 4
  • 22
  • Are you ***absolutely*** sure that it should be `$(Hostname)` and not `$($Hostname)`? – Jeff Zeitlin Jun 04 '21 at 13:17
  • 100% yeah, that's how I have the variable stored in azure and it is evaluated when I run a release pipeline. – DrollDread Jun 04 '21 at 13:24
  • see my update to the question for more context – DrollDread Jun 04 '21 at 13:27
  • It looks like the `az` command/cmdlet may be throwing the error, not your script. Check its syntax/requirements. – Jeff Zeitlin Jun 04 '21 at 13:38
  • Yes, I have read the documentation, and make multiple calls to this function prior to the failed call. There is something with this call that is specifically causing issues. I am already aware that the az command is throwing the error. – DrollDread Jun 04 '21 at 13:41
  • You can also see the string looks as it has split before `\Logs...` so this seems as though there is an issue with this parameter - from what I can tell. – DrollDread Jun 04 '21 at 13:41
  • 1
    That is why I suggested checking the syntax and parameter requirements for `az` - I don't have access to Azure material at the moment, but it looks like `az` may want the parameter to end with the `)`: you may not be able to have multiple substitutions in the string. – Jeff Zeitlin Jun 04 '21 at 13:44
  • ahh I see, apologies. I'm wondering if there's any way around this perhaps. – DrollDread Jun 04 '21 at 13:49
  • https://learn.microsoft.com/en-us/cli/azure/pipelines/variable-group/variable?view=azure-cli-latest#az_pipelines_variable_group_variable_create – DrollDread Jun 04 '21 at 13:49
  • added documentation for reference – DrollDread Jun 04 '21 at 13:49
  • key part: ```--value Value of the variable. For secret variables, if --value parameter is not given, it will be picked from environment variable prefixed with AZURE_DEVOPS_EXT_PIPELINE_VAR_ or user will be prompted to enter it via standard input. e.g. PersonalAccessToken can be input using environment variable AZURE_DEVOPS_EXT_PIPELINE_VAR_PersonalAccessToken.``` – DrollDread Jun 04 '21 at 13:50
  • Nowhere does that say what the syntax of the value of the variable may be. Presumably, `$(STRING)` says that `STRING` is to be substituted with a prescribed value at run time; the question becomes whether multiple substitutions are allowed - the other examples (that succeed) all have at most one such substitution. – Jeff Zeitlin Jun 04 '21 at 13:59
  • Yeah I think you are correct, the end bracket seems to terminate the statement. I'm going to try passing the ASCII value see if that works... – DrollDread Jun 04 '21 at 14:02
  • @JeffZeitlin `$(Hostname)` applies the [subexpression operator `$()`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-5.1#subexpression-operator--) (result of `Hostname` command; in Windows it's `hostname.exe`). – JosefZ Jun 04 '21 at 18:53
  • 1
    @JosefZ - The querent suggests not, in this case, because evaluation is ostensibly being blocked by single-quoting the field. If the string were double-quoted, I'd agree with you. The idea here is that in some config file somewhere, the string '....$(...)...' is a LITERAL entry, rather than being evaluated at write-time. – Jeff Zeitlin Jun 04 '21 at 18:56
  • 1
    This looks like a similar question - https://stackoverflow.com/questions/57980835/how-to-pass-a-variable-into-the-az-pipelines-variable-group-variable-create-co. One answer suggests adding double quotes *inside* the single quotes so the value is parsed by ```az``` correctly - in your case ```'”\\svr-prdfs\$(ASPNETCORE_ENVIRONMENT)\Logs\$(Hostname)\$(Agent.MachineName)”’```. I can’t test that here right now, but it sounds like it might work to me :-). HTH. – mclayton Jun 04 '21 at 23:11
  • @mclayton I have just tested and adding the additional `"` seems to have fixed it! If you would like to submit that as an answer I will mark as correct. – DrollDread Jun 07 '21 at 07:40

1 Answers1

1

Reposting comment as an answer…

This looks like a similar question - How to pass a variable into the 'az pipelines variable-group variable create' command

One answer there suggests adding double quotes inside the single quotes so the value is parsed by az correctly - in your case that would be '”\\svr-prdfs\$(ASPNETCORE_ENVIRONMENT)\Logs\$(Hostname)\$(Agent.MachineName)”’.

I can’t find any definitive documentation describing this behaviour (this is the closest I could find - https://learn.microsoft.com/en-us/cli/azure/use-cli-effectively#using-quotation-marks-in-values), but it seems to work, so hopefully it’ll continue to be supported in future…

mclayton
  • 8,025
  • 2
  • 21
  • 26