Getting error: "Missing ')' in function parameter list." while passing variable in param with hyphen in name in powershell script

huangapple go评论69阅读模式
英文:

Getting error: "Missing ')' in function parameter list." while passing variable in param with hyphen in name in powershell script

问题

抱歉,你提供的脚本中存在一些问题。变量名不能包含连字符,所以你需要将变量名更改为有效的格式,然后在参数声明和脚本中使用这个有效的变量名。以下是修正后的脚本:

param($testName)

$testName = "testName-2-1-dev"   # 此处更改为你的变量名

$updatePermissions = az keyvault set-policy -n $keyvault --secret-permissions get list --object-id $testName
$updatePermissions

请注意,我已将参数的名称更改为$testName,并在脚本中将其初始化为你想要的值("testName-2-1-dev")。这样就不会再出现参数命名问题。

英文:

I have a Powershell script to which I am passing a parameters. The name of one variable has a hyphens in it (variable name format: testName-2-1-dev). But I am getting the error below while executing it:

Missing ')' in function parameter list.

Script:

param($testName-2-1-dev)

$'testName-2-1-dev'   #.....2

$updatePermissions = az keyvault set-policy -n $keyvault  --secret-permissions get list --object-id $testName`-2`-1`-dev
$updatePermissions

I tried to add quotes same as that in 2 and also tried to add ` character before the hyphen, but it did not work out. Can I please get help in this?

答案1

得分: 2

你需要使用花括号 (${...}) 来限定参数/变量名:

param(${testName-2-1-dev})

az keyvault set-policy -n $keyvault --secret-permissions get list --object-id ${testName-2-1-dev}

现在你可以这样调用它:

.\script.ps1 -testName-2-1-dev 123
英文:

You need to use curly brackets (${...}) to qualify the parameter/variable name:

param(${testName-2-1-dev})

az keyvault set-policy -n $keyvault --secret-permissions get list --object-id ${testName-2-1-dev}

Now you can invoke it with:

.\script.ps1 -testName-2-1-dev 123

huangapple
  • 本文由 发表于 2023年6月8日 07:18:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定