Reputation: 141
I'm trying to get my script to set an array as a default value for a script if the value is not given as an argument to my script and seem to be having some issues getting Powershell to understand that it's an array.
I've set params as so:
param (
[string]$games = @('Subnautica', 'Gearcity')
)
The problem seems to be that $games
now gets the value of a string "Subnautica Gearcity". At least that's the output of the variable. The comma seems to disappear and thus so does the array when trying to traverse it using foreach ( $game in $games ) { random jabber }
.
Upvotes: 9
Views: 8807
Reputation: 18649
This bit: [string]$games
forces your variable to be a string. To make it an array of strings you need to add[]
to variable type declaration.
[string[]]$games = @('Subnautica', 'Gearcity')
Upvotes: 7
Reputation: 523
The reason this happens is that you have specified that your Parameter $games
is a string, what you want is an array of strings: string[]
so your code becomes:
param (
[string[]]$games = @('Subnautica', 'Gearcity')
)
Upvotes: 20