Build a web form for a PowerShell script

Image Description

Daily PowerShell #9

Scripting PowerShell Universal Daily PowerShell

October 26, 2021

quote Discuss this Article

PowerShell Universal is a platform for building custom, web-based tools with PowerShell. One of the features of PSU is the ability to build web forms for your PowerShell scripts. In this post, we’ll look at three ways to do this.

Automatic Web Forms with Script Parameters

PowerShell Universal provides the ability to run ad-hoc scripts using the admin console. The platform can parse the basic parameters within the param block of a script and present a form based on the types of parameters shown.

Creating a Form with a Param Block

First, you will need to create a script. Click Automation \ Scripts and then Create New Script. To create a basic form, just specify a param block like you would in any PowerShell script. In the below example, we use various types of parameters.

param(
    $String,
    [string]$String2,
    [string[]]$StringArray,
    [DateTime]$DateTime,
    [bool]$Switch,
    [int]$Number,
    [Switch]$Switch2,
    [System.DayOfWeek]$DayOfWeek
)

$String
$String2
$StringArray
$DateTime
$Switch
$Number
$Switch
$DayOfWeek

When you click Run, you will be presented with a form that contains fields for each parameter.

PowerShell Universal Script Parameters

PowerShell Universal Script Parameters

Once you run the script, you will see the output for each parameter within the Output pane.

Oct 24, 2021 4:23 PM  Test 
Oct 24, 2021 4:23 PM  Test2 
Oct 24, 2021 4:23 PM  test123 
Oct 24, 2021 4:23 PM  test1231 
Oct 24, 2021 4:23 PM  Monday, September 27, 2021 4:23:32 PM 
Oct 24, 2021 4:23 PM  True 
Oct 24, 2021 4:23 PM  123123 
Oct 24, 2021 4:23 PM  True 
Oct 24, 2021 4:23 PM  Thursday 

Help Text

You can include help text within your forms by using the HelpMessage property of the ParameterAttribute.

param(
    [Parameter(HelpMessage = 'Enter your name')]
    $Name
)

$Name

If you hover your cursor over the tooltip, the help message will be shown.

PowerShell Universal Script Help

PowerShell Universal Script Help

Required Parameters

Required parameters can be specified by using the Mandatory property of the ParameterAttribute.

param(
    [Parameter(Mandatory)]
    $Name
)

$Name

Required parameters need to be filled out before the script can be run.

PowerShell Universal Required Parameters

PowerShell Universal Required Parameters

Simple Web Forms in Pages

Pages provide more configuration of the user experience. A form within a page executes a script or API after the user enters the information into the form.

To create a page, click User Interfaces \ Pages and click Create New Page. View the page and then click Edit in the top right.

PowerShell Universal Edit Page

PowerShell Universal Edit Page

Next, click Toolbox and navigate to the Data Input tab.

PowerShell Universal Page Form

PowerShell Universal Page Form

You can resize and reposition the form on the page by dragging the bottom right corner or the middle of the component. Click the blue edit button in the top right to modify the properties of the form. The first setting to ensure that you configure is the script to run. Each field the user enters will be passed as a parameter to the script.

PowerShell Universal Form Target

PowerShell Universal Form Target

Next, you can define the fields for your page. There are 10 field types to choose from including text boxes, check boxes and selects.

PowerShell Universal Form Fields

PowerShell Universal Form Fields

Once you’ve defined your fields, save the form and then save the page. The resulting page will contain the form. Users will be able to enter data and the script will be called.

PowerShell Universal Page Form

PowerShell Universal Page Form

Complex, Dynamic Forms in Dashboards

Dashboards allow for the creation of complex, dynamic websites, including forms, using PowerShell. The Universal Dashboard framework provides over 40 controls to create interactive websites. You can take advantage of the input and form controls to build complex forms.

To create a new dashboard, you can click User Interfaces \ Dashboards and then click Create New Dashboard. Once the dashboard has been created, click the details button. You will see the code editor for specifying the contents of the interface.

The below example is a multi-step form that defines several pages worth of input controls. When using the stepper, you can validate input, adjust steps based on user data and call any PowerShell cmdlet when the form has been submitted.

New-UDStepper -Steps {
    New-UDStep -OnLoad {
        New-UDTextbox -Id 'firstName' -Label "First Name" -Value $EventData.Context.firstName
        New-UDTextbox -Id 'lastName' -Label "Last Name" -Value $EventData.Context.lastName
    } -Label "Name"
    New-UDStep -OnLoad {
        New-UDCheckbox -Id 'email' -Label 'Create Email Address'
    } -Label "Email"
    New-UDStep -OnLoad {
        New-UDDatePicker -Id 'startDate' -Label 'Specify a start date for this employee'
    } -Label "Start Date"
} -OnFinish {
    New-UDTypography -Text 'User has been created!' -Variant h3
    New-UDElement -Tag 'div' -Id 'result' -Content {$Body}
}

The resulting dashboard will contain a single page with the stepper. As you move through the stepper, data will be collected and displayed at the end.

PowerShell Universal Dashboard Stepper

PowerShell Universal Dashboard Stepper

You can use PowerShell Universal for free.