Reputation: 4341
I have two PowerShell Scripts. The one is an initial script which does some work on JSON files and also has some constants in form of an array.
The second one is a function within a separate file.
In my script initial1.ps1
i have the following code:
$IDs=@("02921114-654b-4c28-a9d7-2ebd9ab0ada3",
"9045c61c-55bc-45b0-0000-ec9858b24867",
"011b0c6d-5678-4aaa-a833-e62111103f0a")
. 'D:\...\drop\Add-Folder.ps1' -AdlsAccountName $Accountname `
-PrincipalIds @IDs `
-Path $folderToCreate.path `
-AadGroupName $aadRoleName
My PowerShell script file named 'Add-Folder.ps1` looks like:
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[string] $AdlsAccountName,
[Parameter(Mandatory=$True,Position=2)]
[string[]] $PrincipalIds,
[Parameter(Mandatory=$True,Position=3)]
[string] $Path,
[Parameter(Mandatory=$True,Position=4)]
[string] $AadGroupName
)
Write-Host "Hello from 'Add-AdlsFolderWithPermissions.ps1'!"
Write-Host $AdlsAccountName
Write-Host $PrincipalIds
Write-Host $Path
Write-Host $AadGroupName
But when i execute it i get the following error:
##[error]A positional parameter cannot be found that accepts argument '9045c61c-55bc-45b0-0000-ec9858b24867'.
Why happens this error or how can i pass the array of IDs to my function?
Upvotes: 3
Views: 16115
Reputation: 13191
Using @
with variable name causes parameter splatting, i.e. values in an array are being used as consecutive parameters for the script.
Upvotes: 1
Reputation: 354774
Use $IDs
instead of @IDs
. Otherwise the array will be expanded and attempted to be passed as multiple arguments. This is called splatting.
You can use this to collect either positional arguments to a command in an array, or named arguments in a hashtable, and then invoke the command with that splatted variable. It's never necessary or correct to use it when you actually want to pass an array as a single argument.
Upvotes: 2