Validate Set of Inputs

by Aug 18, 2010

If you need to limit a function parameter to only a set of allowed choices, you should use the next example:

function Get24hEventLogEntries {
param(
[String]
[ValidateSet('System','Application')]
$LogName='System',

[String]
[ValidateSet('Error','Warning','Information')]
$EntryType='Error'
)

Get-EventLog -LogName $LogName -EntryType $EntryType -After ((Get-Date).AddDays(1))

}

You can use Get-24hEventLogEntries to retrieve events written in the past 24 hours. The user can choose to see the 'system' or the 'application' log. EntryType is limited to 'Error,' 'Warning,' or 'Information.'

Twitter This Tip! ReTweet this Tip!