Questions tagged [validateset]

The ValidateSet attribute specifies a set of possible values for a cmdlet parameter argument. This attribute can also be used by Windows PowerShell functions.

The ValidateSet attribute specifies a set of possible values for a cmdlet parameter argument. This attribute can also be used by Windows PowerShell functions.

When this attribute is specified, the Windows PowerShell runtime determines whether the supplied argument for the cmdlet parameter matches an element in the supplied element set. The cmdlet is run only if the parameter argument matches an element in the set. If no match is found, an error is thrown by the Windows PowerShell runtime.

25 questions
8
votes
5 answers

Couldn't use predefined array inside Validateset - Powershell

I'm looking for a way to make a cmdlet which receives parameter and while typing, it prompts suggestions for completion from a predefined array of options. I was trying something like this: $vf = @('Veg', 'Fruit') function Test-ArgumentCompleter { …
barper
  • 99
  • 7
5
votes
1 answer

Powershell [ValidateSet()] Between Separate Parameter Sets Using Same Parameter Name

OK, so I'm trying to write an advanced function that uses two different parameter set names, one is Default the other is TestAccountsOnly. Most of this works fine, however, here's my problem: The output of Get-Help New-SecondaryAccount gives me this…
trebleCode
  • 2,134
  • 19
  • 34
5
votes
2 answers

Powershell multiple function params from ValidateSet

I'm writing a script with PowerShell and at some point I needed to use ValidateSet on function params. It's a very good feature, but what I need is something more than that. For example Function JustAnExample { param( …
SokIsKedu
  • 196
  • 1
  • 2
  • 17
4
votes
2 answers

ValidateSet values with spaces

Here's an example of ValidateSet where the values are strings which contain spaces: function Test-ValidateSet { param ([ValidateSet("abc def", "ghi jkl")][String]$Val) $Val } The IntelliSense works, however the completed value isn't…
dharmatech
  • 8,979
  • 8
  • 42
  • 88
2
votes
3 answers

PowerShell ValidateSet on Boolean Parameter

I am attempting to use ValidateSet with a boolean parameter however I cannot get it to function as expect. An example to replicate the issue: function Set-Boolean { [CmdletBinding()] [OutputType([Bool])] Param ( …
Persistent13
  • 1,522
  • 11
  • 20
2
votes
1 answer

how to get Value from pipeline for a DynamicParam in powershell

My goal is to have a parameter for a powershell functions that supports both ValidateSet (and the tab-complition) for a set that is known only in runtime ability to supply the parameter via the pipeline. I was able to achieve #1, but looks like #2…
rony l
  • 5,798
  • 5
  • 35
  • 56
1
vote
2 answers

Powershell - Why does [ValidateSet("varA", "varB")] not set $LASTEXITCODE?

I wondered that scripts whose parameter are validated by the ValidateSet attribute do not return any invalid $LASTEXITCODE when invalid parameters are given. Let's say I have the following PowerShell script, called try.ps1: #INPUT Variables for the…
Felix Waltl
  • 129
  • 6
1
vote
1 answer

How to prevent PowerShell validateSet argument completer from suggesting the same values that are already selected?

I have 2 class-based argument completers/validate sets for 2 of my PowerShell module's parameters. [ValidateSet([PolicyIDz])][parameter(Mandatory = $false, ParameterSetName = "Remove…
1
vote
2 answers

Using ValidateSet with functions or List

I'm trying to do something like this. The Get-SomeOtherParameter returns a system.Array type list from a database. I don't want to hardcode my ValidateSet in case the list changes overTime in the database function Get-SomeItems { param ( …
1
vote
2 answers

PowerShell - Is there a way to create a sort of 'alias' that contains multiple parameter of a validateset?

Good afternoon, I recently created a PowerShell script to automate and test PowerCLI scripts before running them on vCenters/ESXi hosts. At the moment, I use a validate set to let the user chose on which server/cluster they want to execute their…
Dh3va
  • 41
  • 4
1
vote
1 answer

Powershell - Trouble passing Validateset parameters into ForEach-Object, getting prompt

So I'm new to PowerShell, and I'm trying to get this function to work. I have 2 ValidateSet arrays with 3 parameters. These parameters are supposed to change the file path and copy them over from one server to another. For some reason, I keep…
1
vote
2 answers

How to have a Powershell validatescript parameter pulling from an array?

I have a module with a lot of advanced functions. I need to use a long list of ValidateSet parameters. I would like to put the whole list of possible parameters in an array and then use that array in the functions themselves. How can I pull the…
silverbackbg
  • 183
  • 2
  • 15
1
vote
0 answers

Getting selected value of a parameter to provide values for next parameter in Powershell

I am writing a function where I am planning to provide dynamic parameter auto-completion. Idea is this; When I type Activate -User (-User gets auto-completed by GetUsers class) I wanna use selected by -User parameter in…
verte
  • 49
  • 1
  • 2
1
vote
1 answer

Dynamic Parameter Doesn't Show After Using Validated Set Parameter

I've been playing about with my new New-CompanyADUser cmdlet in our Company-AD module, and I'm experiencing a weird problem with my dynamic parameter: it won't appear at all after I've chosen an option within a validated set parameter that has more…
1
vote
1 answer

Conditional Mandatory in PowerShell

I'm trying to make a parameter mandatory, but only if another parameter uses certain ValidateSet values. It seems that using a code block on Mandatory doesn't work as expected. function Test-Me { [CmdletBinding()] Param ( …
srbrills
  • 1,451
  • 2
  • 13
  • 33
1
2