0% found this document useful (0 votes)
26 views5 pages

A PowerShell Form Generator

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

A PowerShell Form Generator

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

[Link]

com/Tips/770313/A-PowerShell-Form-Generator

A PowerShell Form Generator


A PowerShell Form Generator.
This article is an evolution of the previous one, it regards not only the implementation of
the package, but in particular shows the new paradigm with which the form objects are
specified and the interaction with the WEB.
 Download PowerShell Form Generator

Introduction
This article is an evolution of the previous, it regards not only the implementation of the
package, but in particular a new paradigm with which the form objects are specified; the
article is not intended to be an exhaustive explanation of the product, for this please
refer to the attached documentation.

Whit the experience gained over time on Form Generator (see the list here), I hope to
have achieved a more lean package structure as well as a simpler description of the
form objects.

The script has been developed in Windows 11 with Powershell version 7.3.7.

Using the program


The script formgen.psm1 contains the function formGen that, when invoked, generates a
form and returns the fGen object.

formGen is invoked with the parameters:

 Widgets a mandatory string containing a list of widget description;


 CallBack function a function that receive, when the form is closed, the data
contained in the form;
 Background a possibly background color of the form.
PS C:\Sviluppo\PowerShell> using module .\formgen.psm1
PS C:\Sviluppo\PowerShell> $parm = @"
Form '' dialog
Text psw Password password
Text repeatPassword '' password
Text mail
cmb Language '' IT=Italian,FR=French,EN=English,SP=Spanish value FR
Chk check '' 'Consent send info'
Control psw -eq repeatPassword 'Passwords not equals'
"@
PS C:\Sviluppo\PowerShell> $d = formgen $parm -background cyan
PS C:\Sviluppo\PowerShell> $d.fg_data
Name Value
psw EllenikePoliteia
mail ellen@[Link]
Language FR
check On
fg_form
fg_button fg_Ok
repeatPassword EllenikePoliteia

Every control or widget description is characterized by a list of attributes separated by


space(s) in this order:
Type, Field Name, Field Label and Extra(s). Every description starts in a new line.

The Extra field consist of a single parameter or a parameter value pair, some are
common to all or nearly all widget like Default, ToolTip, Call function [parameter]
etc... others are specific to widget for
example After widget, Center, Color, Format etc...

Furthermore, other informations, called pseudo type, have a slightly different syntax and
they serve, among other things, to improve the presentation and control of the data.

The program inserts the buttons: Ok, Reset and Cancel; however if there is only one
widget, no buttons are presents because the choice on the item or the item inserted
exits the form.

Closing the form, the global variable $fg_data, an hash table where the key is the
widget name, contains the input inserted plus a key fg_button whit the name of the
button clicked, fg_Start and fg_End for timing purpose however, if the cancel button
was pressed there is only fg_Start, fg_End and fg_button.

The widgets
 Buttons
 Comments
 Check boxes
 Lists
 Menu
 Radio buttons
 Text fields i.e text, password, file, date, protected field ...
 Timers
 Track bars

There are no particular things to tell for widget type Text, password or check box. The
data for the items of Lists, Combos and RadioButtons are contained in the extra field
each separated by comma; every item can have a possible key, which is returned if the
item is selected.
The Pseudo Types
This is perhaps the most interesting part as the pseudo types, as already mentioned,
they relate to the presentation of the form (Window and Label) or the event management
(Event, Get, Timer) or for validate the data (Control and Required).

Data control

The Control command can perform comparison operations between fields and constants
or formal checks via regular expressions, while the Required command imposes the
presence of the data.

The controls occur when the form is closed.

T wTime 'Waiting time' Numeric Width 100


Control wTime -gt 0 'Waiting time must be greater 0'
Control wTime -le 1000 'Waiting time too long'
Text e_mail Mail
Control e_mail is mail 'Incorrect mail form'
Slider Min 'Min pressure' From 30 To 150
Slider Max 'Max pressure' From 40 To 220
Control Min -lt Max 'Minimum must be less of Maximum'
T Password '' Password
Control Password is '(?=.*\d)(?=.*[a-z]).{8,}' 'Almost eight character one numeric'
Required e_mail Password wTime

Form presentation

The Window, or the alias Form, command can provide a title, a background color, a
position on the screen and the modality (dialogue and permanence); the Labels can be
colored and right aligned by the Label command.

Buttons, check boxes and comments can be positioned after another widget.

Event management

Almost all widgets can include a call parameter which, associated with an event on the
widget, calls a function (or starts an Internet transaction); the event in question is the
most usual for the widget: Click for buttons, KeyDown with Enter key for texts ...
Other events can be set by the command Event; in the example below the
event LostFocus invokes the control on the field when he loses focus (as well as when
the form is submitted).

Window Controls background #FF00C0C0


label right color blue after ' :'
T wTime 'Waiting time' Numeric Width 100
Text e_mail Mail
Slider Min 'Min pressure' From 30 To 150
Slider Max 'Max pressure' From 40 To 220
Control Min -lt Max 'Minimum must be less of Maximum'
T Password '' hint 'Almost 8 characters' Password
Control Password is '^.{8,}$' 'Almost 8 characters'
Event LostFocus on Password Call fg_wdgControl %%Password
Control e_mail is mail 'Incorrect mail form'
Control wTime -ge 0 'Number must be greater -1'
Control wTime -le 1000 'waiting too long'
Required e_mail Password wTime
WEB access

FormGen can send and receive data from the WEB.


The pseudo type Get asks data from internet to populate the form, also
the Call parameter can interact with the WEB, the commands syntax are:

 GET *|name URI [Every seconds]


 ... Call URI Answer FieldName [Send fieldList]

formGen send an Internet request that depends on the presence of Answer or Send sub
parameter. Answer generates a GET request for receive data; Send generates a POST
request for transfers the fields indicated including also any file uploads.

The Get commands are executed before the form is displayed; the Call are activate by
event.

# Web *****
List Languages
C Time ' ' after Languages
List Hellas
CMB Town
File File '' filter 'Graphic files(*.jpg;*.png;*.gif)|*.jpg;*.png;*.gif|Texts(*.txt)|
*.txt' title 'Graphic file'
T Answer '' Width 501
H URI [Link]
H URI2 [Link]
H URI3 [Link]
H fields Time,Town,Languages,Hellas,Answer,File
B getQuote Quote Call %%URI Answer Answer After Answer
B Send '' Call %%URI3 Answer fg_HTML send %%fields ToolTip 'Send form data'
Get * [Link]
Get Town [Link]
Get Hellas [Link]
GET Time %%URI2 Every %minute
GET Languages [Link]
Timer Timer every 15 delay 20 Call %%URI Answer Answer
Required File

The response time of the Get command depends on the response of the WEB server, to
minimize the response time formGen sends the command concurrently:

PowerShell
# handle gets ***********
$GetScript = {
Param ([string]$URI,[string]$field)
try {
$answer = Invoke-RestMethod -Method 'GET' -Uri $URI -SkipHttpErrorCheck
} catch {
$answer = $_.[Link]
}
"{$field}$answer"
}
foreach ($h in $[Link]) {
$x = Start-Job $GetScript -ArgumentList $[Link]($gets[$h]), $h
}
$aGets = Get-Job | Wait-Job | Receive-Job
Foreach ($item in $aGets) {
...
Every Get command invoke a background job ($GetScript) by means of Start-
Job command; the responses are accumulated in the $aGets array by Receive-
Job command when all Jobs (Get-Job) are finished (Wait-Job).

An example
The example is based on generation of QRCodes obtained through the
module QRCode.ps1 that i s a wrapper for the module QRCodeGenerator for generate
QRCode for Geolocation, Text, Twitter, URI and VCard.
The script extracts the fields necessary for the chosen QRCode from the syntax of the
call in QRCodeGenerator module (URI QRCode below):

PS C:\Sviluppo\PowerShell> Get-Command -Module QRCodeGenerator -name New-QRCodeURI -


syntax
New-QRCodeURI (alias) -> New-PSOneQRCodeURI

New-QRCodeURI [-URI] <uri> [[-Width] <int>] [[-OutPath] <string>] [[-DarkColorRgba]


<byte[]>]
[[-LightColorRgba] <byte[]>] [-Show] [<CommonParameters>]

Note for user


In the attached documentation in addition to the formGen module ( formgen.psm1) there
are the script sandbox.ps1 which allows complete exploration of the form generator
through some examples; it is sufficient to extract the contents into a folder and start the
batch script [Link].

Furthermore, the QRCode.ps1 script allows you to generate QRCodes such as texts,
websites and geolocations.

☞ The execution depends on machine execution policy that can be bypassed for the
session by the command: Set-ExecutionPolicy Bypass -scope Process -Force;

You might also like