using Enums as parameters in a script

Tagged: 

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Thom Schumacher Thom Schumacher 1 year, 6 months ago.

  • Author
    Posts
  • #33387
    Profile photo of Thom Schumacher
    Thom Schumacher
    Participant

    Is there a way to use an enumeration from a C# set of code for instance this enumeration So that I can use this enumeration as an input to a script to create a sql job:

    [enum]::GetNames([Microsoft.SqlServer.Management.Smo.Agent.agentsubsystem])

    Produces the following names:
    TransactSql
    ActiveScripting
    CmdExec
    Snapshot
    LogReader
    Distribution
    Merge
    QueueReader
    AnalysisQuery
    AnalysisCommand
    Ssis
    PowerShell

    How can I use this in a param block so the valid values are from the enumeration?

    I know with parameter set I can do ValidateSet but I want it to be dynamic.

  • #33390
    Profile photo of Peter Jurgens
    Peter Jurgens
    Participant

    You can specify the data type for your parameter as the enum type and it will even give intellisense for the type names. I don't have that assembly on my computer to test but here's an example using system.consolecolor:

    function test {
    [cmdletbinding()]
    param(
    [system.consolecolor]$color
    )
    }
    
  • #33398
    Profile photo of Thom Schumacher
    Thom Schumacher
    Participant

    thank you that worked perfectly. exactly what I was looking for.

    function test-function
    {
      param([Microsoft.SqlServer.Management.Smo.Agent.agentsubsystem]$name)
        out-host $name
     }
    

You must be logged in to reply to this topic.