Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use ValidateSet with the contents loaded from a CSV file

I really like the way that ValidateSet works. It proposes the options as a list while you type your Cmdlet in the PowerShell ISE.

I would like to know if it's possible to retrieve values from a CSV-file (Import-CSV) and use them in the Param block so they become available in the drop down box of the PowerShell ISE when constructing the Cmdlet arguments? A bit in the same way that $Type works now, but then with values from the import file.

Function New-Name {
Param (
    [parameter(Position=0, Mandatory=$true)]
    [ValidateSet('Mailbox','Distribution','Folder','Role')]
    [String]$Type,
    [parameter(Position=1,Mandatory=$true)]
    [String]$Name
)
    Process { 'Foo' }
}
like image 917
DarkLite1 Avatar asked Aug 07 '14 08:08

DarkLite1


People also ask

What is Validateset in PowerShell?

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

What does import-CSV do in PowerShell?

The Import-Csv cmdlet creates table-like custom objects from the items in CSV files. Each column in the CSV file becomes a property of the custom object and the items in rows become the property values. Import-Csv works on any CSV file, including files that are generated by the Export-Csv cmdlet.


2 Answers

Here is something you can start with:

function New-Name {
    param (
        [parameter(Position=0, Mandatory=$true)]
        [String]$Name
    )

    dynamicparam {
        $attributes = new-object System.Management.Automation.ParameterAttribute
        $attributes.ParameterSetName = "__AllParameterSets"
        $attributes.Mandatory = $true
        $attributeCollection = new-object -Type System.Collections.ObjectModel.Collection[System.Attribute]
        $attributeCollection.Add($attributes)
        $values = @('MailBox', 'Tralala', 'Trilili') # your Import-Csv here
        $ValidateSet = new-object System.Management.Automation.ValidateSetAttribute($values)
        $attributeCollection.Add($ValidateSet)

        $dynParam1 = new-object -Type System.Management.Automation.RuntimeDefinedParameter("Type", [string], $attributeCollection)
        $paramDictionary = new-object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
        $paramDictionary.Add("Type", $dynParam1)
        return $paramDictionary 
    }

     process { 'Foo' }
}

Credits where credits are due, this largely comes from the following article from the Scripting Guy. The code isn't pretty, but it does what you want.

completion

like image 60
David Brabant Avatar answered Oct 23 '22 02:10

David Brabant


I know this post is quite old, but with Powershell 6.2 and above you can use a .NET class at the beginning of the script and have the set controlled by a csv for example.

This article hear does an excellent job of explaining: https://adamtheautomator.com/powershell-validateset/

like image 1
beezkneez Avatar answered Oct 23 '22 01:10

beezkneez