Author Posts

December 30, 2015 at 12:26 pm

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.

December 30, 2015 at 1:36 pm

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
)
}

December 31, 2015 at 6:01 am

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
 }