0% found this document useful (0 votes)
394 views

Using PrimalForms Community Edition

Using Primal Forms

Uploaded by

Gabriel Sari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
394 views

Using PrimalForms Community Edition

Using Primal Forms

Uploaded by

Gabriel Sari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Copyright © 2009 SAPIEN Technologies, Inc.

All Rights Reserved

Adding a graphical interface to your PowerShell scripts is easy, fun and


free with PrimalsForms Community Edition/ Guide
Creating Graphical PowerShell Tools
with PrimalForms Community Edition
In the past, creating a PowerShell nect to the computer and displays the property panel for each con-
script using Windows Forms to when the computer last booted trol that you drag and drop.
create a graphical interface was a and how long it has been run-
very tedious task and rarely used ning. Here’s how easy it is to Click the form itself. In the prop-
except for the most simplest of create a script like this. erty panel you will likely want to
forms. No more. The free Pri- change the default form title,
malForms Community Edition Before we can begin you’ll need Primal Form to something else.
tool lets you create very rich to go to the Downloads section Edit the Text property. I changed
Windows Forms-based Power- of www.PrimalTools.com and mine to Boot Time. You can also
Shell scripts using a WYSIWYG get PrimalForms. I’m assuming change the BackColor property
editor. Drag and drop the form you already have PowerShell in- as I did to yellow.
elements, define your event han- stalled. PrimalForms will work
dlers, export the form to a file, with either PowerShell v1.0 or Drag a label control to the form.
v2.0. Notice you can drag and reposi-
tion anywhere on the form.
Change the text to: Enter a com-
Creating the Form  putername:. Of course we need

Start Primal-
Forms and
click New to
create a new
form. On the
left side are all
of the form
add your PowerShell code and elements such
run your script. The Boot Time as buttons and
form you see is a PowerShell text boxes that
script I created with Primal- you can add to
Forms. The script uses the com- your form. On
putername in the text box to con- the right side is

SAPIEN Technologies Inc. | 841 Latour Court, Suite D | Napa, CA 94558 | www.sapien.com | 707.252.8700 1
Copyright © 2009 SAPIEN Technologies, Inc. All Rights Reserved

Guide
some place for the user to enter a Next I need some buttons for the You can define events for all
computer name. For that we’ll user to click, so drag a button sorts of actions such as selecting
use a TextBox. Grab one and control and position it on the an item from a drop down box to
drag it onto the form. Position form. As with the label control, I when a form is first shown.
and resize as you wish. change the control name from
button1 to something like btnGo. The last item to add to the form
If you look at the property panel Change the text that appears on is a place to display the results.
for the text box, you’ll see it has the button as well. I created but- For that I’ll use a Rich Text Box
a default name like TextBox1. I tons to get boot time information control positioned underneath the
prefer to give con- and to close the form. But text box. If you don’t want the
trols more mea- what happens when a user border, simply change the
ningful names be- clicks the button? When BorderStyle property to None.
cause I will refer- that happens, a Click event
ence this object in is fired. It’s up to you to It doesn’t take long to lay out the
the PowerShell decide what happens. form controls, define properties
script by this and events. It’s now time to add
name. Something Select the Go button. On PowerShell code to make the
like txtComputer- the property panel click the form do something. PrimalForms
name is more lightning bolt icon. This Community Edition will generate
meaningful be- will display the event hand- a PowerShell script to display the
cause I can tell ler properties. You may see graphical Windows Form ele-
from looking at different properties de- ments which would otherwise be
the name that it is pending on the selected an extremely tedious process if
a text box control control. For a button, enter you had to manually develop the
and I have a better something for the Click script from scratch.
idea of which one. event. This will become the
When the Power- name of the corresponding You can either export the form to
Shell code is gen- PowerShell scriptblock. In a PowerShell script file, export it
erated for the form, this property the graphic above I’ve defined a to the clipboard, or if you have
name will become a variable scriptblock called GetTime. Later PrimalScript 2009 installed, ex-
named $txtComputername. I’ll add PowerShell commands in port to a file and edit.
my script editor to this
I’m also going to give the text scriptblock.
box a default value of the local Adding PowerShell 
computername. Because I’ll be Technically you don’t have to
running a PowerShell script, I define a click event handler, When you look at the Primal-
can use PowerShell expressions PrimalForms will automatically Form generated script, most of
like $env:computername which create one for each button, but I the code is for generating the
will return the value for the prefer to use more descriptive form elements. You shouldn’t
%Computername% environmen- names instead of have to edit any of that code. In-
tal variable. bntGo_OnClick. But you don’t stead, you need to create the code
have to. for your event handlers, which
I’ll cover in a moment.

SAPIEN Technologies Inc. | 841 Latour Court, Suite D | Napa, CA 94558 | www.sapien.com | 707.252.8700 2
Copyright © 2009 SAPIEN Technologies, Inc. All Rights Reserved

Guide
The script is essentially a func- If you realize you need to modify ring, by first copying my script
tion that runs the form. You can the graphical elements you’ll block, then using Ctrl+Shift+V
insert any other functions or Po- need to reopen your PrimalForms to cycle through until I find the
werShell commands you require project, generate a new script and text I need.
when you begin developing more re-add your PowerShell code.
advanced scripts. But for now all Another technique comes from
we need to edit is a scriptblock However there are a few tech- the SAPIEN support forums.
like this: niques for creating scripts and Create a script file from Primal-
moving between the code gener- Forms. The next time you need
#---------------------------------------------- ated by to modify the form and script,
#Generated Event Script Blocks Primal- open the script in PrimalScript
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms. Forms 2009. Modify the form and ex-
$GetBootTime= Community port the result to a file. In Pri-
{
#TODO: Place custom script here Edition and malScript, use the compare files
}
the script feature to compare your script
you create with the exported file. Primal-
You can write whatever Power- in Primal- Script will highlight all the dif-
Shell code you want within the Script. One technique that I’ve ferences. All you have to do is
scriptblock. You can access the started using is to leverage Pri- copy and paste the differences
form controls and their properties malScript’s snippet feature. from the exported file to your
as well. In the script you’ll find script. This can be made even
variables defined for all of the Like many of you I’ll start with a easier by splitting the window so
controls. basic form and begin building a that you can see your script and
script. Inevitably I have to go the compare files tab. Select text
then drag and drop it into your
#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
script.
$statusBar1 = New-Object System.Windows.Forms.StatusBar
$rtbResults = New-Object System.Windows.Forms.RichTextBox
$btnGo = New-Object System.Windows.Forms.Button
When creating your scriptblocks,
$txtComputername = New-Object System.Windows.Forms.TextBox I recommend first developing a
$label1 = New-Object System.Windows.Forms.Label
$btnQuit = New-Object System.Windows.Forms.Button standalone PowerShell script or
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState function. Once you know your
#endregion Generated Form Objects
PowerShell code works, you can
PrimalScript will provide Pri- back and modify the form. Be- insert it into your PrimalForms’
malSense for any defined Win- fore I do so, I select my script script and edit as necessary to
Form element which makes block section, right click, and use work with the graphical controls.
script development pretty easy. the Save as Snippet feature. In This is important because if the
PrimalForms Community Edition PrimalForms, I copy the script to forms based version of the script
includes a Getting Started Guide the clipboard, return to Primal- has problems I can trust that my
with a number of sample forms Script and paste into my script basic PowerShell expressions are
and scripts which you can use as file. I select and delete the script valid and problems are most like-
starting points for your own block. In its place I insert my ly related to using the form ele-
projects or as teaching examples. saved snippet. A variation is to ments.
use PrimalScript’s clipboard

SAPIEN Technologies Inc. | 841 Latour Court, Suite D | Napa, CA 94558 | www.sapien.com | 707.252.8700 3
Copyright © 2009 SAPIEN Technologies, Inc. All Rights Reserved

Guide
For the boot time form I already quired wherever the file will be When you are ready, not only
had a short PowerShell script that running. can you create a standalone Po-
worked. Here’s my original code: werShell script, but you can also
package it into an executable di-
rectly from
$computername=$env:computername Primal-
$os=Get-WmiObject win32_operatingsystem -computername $computername Forms
2009.
Write-Host ("Last boot: {0}" -f $os.ConvertToDateTime($os.lastbootuptime))
Write-Host ("Uptime : {0}" -f ((get-date) - os.ConvertToDateTime($os.lastbootuptime)).tostring())
If you’d
I will modify this code and add it like to see
to the script block that executes PrimalForms 2009  more, download an evaluation
when the Get Time button is copy from PrimalTools.com. As
clicked. Modifications are pretty PrimalForms Community Edition always, if you require some as-
simple. I need is terrific at gene- sistance developing your Power-
$computername=$txtComputername.Text rating PowerShell
to use the val- $os=Get-WmiObject Shell Script please join us at
ue from the win32_operatingsystem -computername scripts that display ScriptingAnswers.com.
$computername -ea SilentlyContinue
text box for Windows forms
the $compu- and controls. How- Jeffery Hicks
tername varia- ever, adding Po-
ble. werShell code for the associated
events requires a script editor
The same Get-WMIObject ex- like PrimalScript.
pression is executed. However
instead of writing results to the If you’d like a more integrated
console, the results will be writ- solution take a look at Primal-
ten to the rich text box control. Forms 2009, the commercial
cousin to the Community Edi-
tion. Creating
$line1="Last boot: {0}" -f the graphical
$os.ConvertToDateTime($os.lastbootuptime)
$line2=$rtbResults.text="Uptime : {0}" -f ((get-date)
form is just as
- $os.ConvertToDateTime($os.lastbootuptime)).tostring() easy; however
$rtbResults.Text=$line1+ "`n" + $line2
you can also
create and edit
I save the script and test it in your
PowerShell. Now instead of a scriptblocks with the help of an
plain console based script I have integrated editor complete with
a graphical tool I can deploy to help and PrimalSense. Primal-
any machine configured to run Forms 2009 also allows you to
PowerShell scripts. I can even preview your form or even run it
use PrimalScript’s Script Pack- right from the editor.
ager to create an exe file, al-
though PowerShell is still re-

SAPIEN Technologies Inc. | 841 Latour Court, Suite D | Napa, CA 94558 | www.sapien.com | 707.252.8700 4

You might also like