Need Help Structuring Parameters in a PowerShell Script

0
11
Asked By HappyCoder42 On

Hey everyone! I'm working on a utility script that needs to handle different types of parameters in a flexible way. The script requires one mandatory positional parameter for the environment and has an optional positional parameter for color, plus a non-positional parameter that should accept multiple inputs without needing any special format. Here's how I've set up my parameter block:

```pwsh
param (
[parameter(Position = 0, Mandatory = $true)]
[ValidateSet("dev", "uat", "prd")]
[String]$env,

[parameter(Position = 1, Mandatory = $false)]
[ValidateSet("blue", "green")]
[String]$colour = "",

[parameter(Mandatory = $false, ValueFromRemainingArguments = $true)]
[String[]]$targets
)
```

The idea is that users can input targets as space-separated values like `-targets one two`. However, when I run the script without specifying the second optional parameter, I get an error related to the color validation. I've realized I could combine the environment and color parameters, but I'd really like the flexibility for users. Also, bonus points if it can handle formats like `this.might.be.one["too"]` easily. Any advice on how to structure this? Thanks!

5 Answers

Answered By PowershellGuru On

I'd suggest looking at argument mode in PowerShell. In this mode, you don’t actually need to encapsulate parameters like `targets` in an array with `@(...)`. Just using commas will suffice, which simplifies your command a lot. For example: `script.ps1 dev -targets one, two` can be used instead, without the extra complexity.

ScriptMaster123 -

Totally agree! I didn’t realize arrays didn’t need that syntax. Super helpful!

Answered By CodingNinja On

Maybe consider using a file for your targets instead? That could simplify things and reduce command line clutter, though I get it might add steps to your workflow if it’s not needed.

HappyCoder42 -

That’s a great idea, but it might slow me down. Just pasting text into the CLI feels quicker!

Answered By TechieTommy On

It sounds like you might need to tweak how your -targets parameter is structured. Instead of just space-separated inputs, try using comma-separated values when you call the script, so like: `script.ps1 dev -targets one, two`. This way, it recognizes them as part of a single array input!

UserFeedback99 -

I gave that a shot, and it actually worked! But I'd prefer not to add extra commas if I can avoid it.

Answered By DevOpsDude On

What’s actually driving you to have these optional positional parameters? There could be simpler ways to structure your script or functions, maybe even a wrapper for the calls? Providing a bit more context on your overall goal could lead to better suggestions!

HappyCoder42 -

Great point! The script is meant to streamline local Terraform development across various environments, so I need to specify the correct environment and color easily. I want to keep it user-friendly.

Answered By QuickFixPro On

If you want the targets parameter to accept multiple values without needing them wrapped in a string, just restructure your parameters. Make sure `ValueFromRemainingArguments` is correctly applied. It could look something like this:

```pwsh
param (
[String]$env,
[String]$colour,
[String[]]$targets,$_
)
```

And you can pass `-targets one two` directly without needing additional squiggles! Just remember the order is crucial!

HappyCoder42 -

Thanks for that tip! I see the possibilities. I guess I was just overcomplicating it.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.