1

I am getting the following error

New-AzResourceGroup : A positional parameter cannot be found that accepts argument 't'.
At line:1 char:1
+ New-AzResourceGroup -Name @rgName -Location @location -Tag @{LoB="pla ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [New-AzResourceGroup], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGrou
pCmdlet

while trying to create a new resource group with the following code. Where is the issue?

$rgName = "storage-dev-rg"
$location = "eastus"
New-AzResourceGroup -Name @rgName -Location @location -Tag @{LoB="platform"; CostCenter="IT"}
mklement0
  • 382,024
  • 64
  • 607
  • 775
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327

2 Answers2

1

To quote your own answer:

The declared variables should be referenced using $, not with @.

about_Variables explains that in order to create and later reference variables in PowerShell, you prefix their name with sigil $ in both cases; i.e., $rgName and $location in your case.

You only ever prefix a variable name with sigil @ if you want to perform splatting (see about_Splatting).

(The sigil @ has other uses too, namely as @(...), the array-subexpression operator, and as @{ ... }, a hashtable literal, as also used in your command.)

Splatting is used to pass an array-like value stored in a variable as individual positional arguments or, more typically, to bind the entries of a hashtable containing parameter name-value pairs to the parameters so named - see this answer.

Since your variables contain strings and strings can be treated as an array-like collection of characters (via the System.Collections.IEnumerable interface), splatting a string variable effectively passes each character as a separate, positional argument.

PS> $foo = 'bar'; Write-Output @foo # same as: Write-Output 'b' 'a' 'r'
b
a
r

As for what you tried:

-Name @rgName, based on $rgName containing string 'storage-dev-rg', passed 's' - the 1st char only - to -Name, and the remaining characters as individual, positional arguments. 't', the 2nd character, was the first such positional argument, and since New-AzResourceGroup didn't expect any positional arguments, it complained about it.

mklement0
  • 382,024
  • 64
  • 607
  • 775
0

I figured it out. The declared variables should be referenced using $, not with @.

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327