you would then enumerate $users by using a ForEach construct. This is a much better pattern than what you're doing, as it allows for 0-infinite number of inputs. This is also how virtually every native command accepts collections of input.

It's easy enough to enumerate through $data, then. If each run of the script represents a single user, then $data will contain keys for $data.Name, $data.Country, and whatever else exists. You can use $data.KeyExists('Name') to determine if a key exists or not.

And $thing would be a hash table, with keys, the KeyExists() method, etc.

This way, each element has a name – you're not just relying on positional input, which is hard to maintain and read. Each element can have variable keys, so not every element needs to be defined.

Part of your original problem, incidentally, is this whole positional thing. You're coding this like it's C#. Even if you had:

Param(
$name,
$city,
$state,
$department
)

You don't have "blanks" per se. You're only thinking that because you're using positional input rather than named input.

MyScript.ps1 -name Don -city Vegas -department "Info tech"

That approach – which is how PowerShell's really meant to work – means I can safely omit State. I'm explicitly naming each piece of input, so PowerShell knows what goes where. So you could go that approach as well, if you like.

Author

Posts

The topic ‘Passing parameters containing blanks’ is closed to new replies.