DrollDread
DrollDread

Reputation: 371

String was unexpected at this time

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

Upvotes: 1

Views: 1166

Answers (1)

mclayton
mclayton

Reputation: 10075

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…

Upvotes: 1

Related Questions