I wrote a PowerShell script which accepts a string array parameter. It is defined in the param section of the script like this:
param
(
[Parameter(Position=0)]
[string[]]$TemplateNames,
...
)
When calling the script via a PowerShell command-line, I can easily pass multiple strings to this parameter by using the following syntax:
.\Script.ps1 -TemplateNames @("Name1","Name2")
However, I need to call this script in a scheduled task and no matter what syntax I use, I can't get PowerShell to interpret the array correctly. The @(...) syntax doesn't work, passing the strings separated with a comma also doesn't work, putting the array in brackets also doesn't work and no matter how I escape the parameter or even the entire script call in single quotes, double quotes or even multiple single or double quotes, it just won't work. The same problem occurs also when calling the script manually via the Windows command line like this:
Powershell.exe -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File "C:\Script\Script.ps1" -TemplateNames ...
How can I pass an array to a PowerShell script in a scheduled task or when calling it via the Windows command line?