Parameters
Parameters for PowerShell Universal jobs.
Last updated
Parameters for PowerShell Universal jobs.
Last updated
Jobs support automatically generating forms with parameters based on your script's param
block. The type of control will change based on the type you define in the block. Parameters that are mandatory will also be required by the UI.
Parameters can be simply defined without any type of parameter attribute and they will show up as text boxes in the UI.
Universal supports various types of parameters. You can use String, String[], Int, DateTime, Boolean, Switch and Enum types.
You can define string parameters by specifying the [String]
type of by not specifying a type at all. Strings will generate a textbox.
You can specify string arrays by using the [String[]]
type specifier. String arrays will generate a multi-tag select box.
You can use the [DateTime]
type specifier to create a date and time selector.
You can use a [Bool]
type selector to create a switch.
You can define a number selector by using the [Int]
type specifier.
You can define a switch parameter using the [Switch]
type specifier to create a switch.
You can use System.Enum values to create select boxes. For example, you could use the System.DayOrWeek
to create a day of the week selection box.
When you specify a PSCredential
, the user will be presented with a drop down of credentials available as variables.
You can allow users to upload files by using the [File]
type.
Files will be available as a PSUFile
object in your scripts. This object has a byte[]
array that you can use to process the file.
For example, you can get the string content for the file by converting it using the Encoding classes.
You can use the DisplayNameAtrribute
to set a display name for the script parameter.
You can define help messages for your parameters by using the HelpMessage
property of the Parameter
attribute.
You can use the Parameter attribute to define required parameters.
You can use both static and default values for parameters. The default value is calculated when the job is about to be run.
The ValidateSet
attribute is used to enforce which values can be passed to a parameter. Learn about ValidateSet here. PowerShell Universal will automatically create a drop-down menu with the values provided to the ValidateSet
attribute for parameters.
For example, a script could define a param block as follows.
The result is shown below.
You can pass parameters from PowerShell using the Invoke-PSUScript
cmdlet. This cmdlet supports dynamic parameters. If you have a param
block on your script, these parameters will automatically be added to Invoke-PSUScript
.
For example, I had a script named Script1.ps1 and the contents were are follows.
I could then invoke that script using this syntax.
The result would be that Hello was output in the job log and pipeline.
PowerShell Universal supports parameter sets. When a parameter set is defined, a drop down is provided that allows for switching between the sets.