Primal Forms Primer IV – Combo Box

I decided that I wanted to be able to pick a server from a list and do something with it. I found a Sample script called SystemInformastion.pff that had this ability. I could load my list of servers from AD easily enough but where I am working now, that brings up a list of thousands of computers. As my department only has about 25 server to worry about, I chose to use a simple text file for input. This has the benefit of allowing me to add or subtract servers from my list without changing my code. The way it is implemented in the sample script, I can have several different list if I need them. A win-win for me.

So the first thing we do is pick the list we want to use by adding this code to our script:


function OnApplicationLoad {
#Note: This function runs before the form is created
#TODO: Add snapins and custom code to validate the application load
    $script:list = Read-Host "Please enter the list file path (Default: c:\computers.txt)"
    if($script:list -eq $null -or $script:list.Length -eq 0)
    {
        $script:list = "c:\computers.txt"
    }
if (!(get-item $global:list -ea "silentlycontinue").exists) {

    Write-Warning "Failed to find $list"
    #exit the script
        return $false
    }

    return $true #return true for success or false for failure
}

Next we need to populate our ComboBox with the server names;


$PopulateList={
    $statusBar1.text="Getting computer names from $list"
    Get-Content $list | foreach {
    $comboServers.items.add($_.Trim())

    $statusBar1.Text="Ready"
    }
}

In the Forms designer, drag a ComboBox on to your form and Name it comboServers if you are using my code. In the properties menu, select the Lightning bolt and then go down to Behavior and select Load. Select PopulateList ( the function we just added) from the drop down box.

We now have our list of servers to select from, so now what?

Drag a status bar and a richTextBox over on to your form. Place the RichText box artistically on your form, below your Server List combobox. And name it rtbPerfData in the Properties window, under design. Your status bar is fine the way it is.

In the Script editor, add this code;


$GetData={
    $statusBar1.text="Getting Connection information for {0}" -f $comboServers.SelectedItem.toUpper()
    $target = $comboServers.SelectedItem.toUpper()
    #clear values
    $FQDN=$Null
    $IPAddr=$Null
    $IP=$Null
    $formTestConnection.Refresh()
    #turn off the error pipeline
    $errorActionPreference="SilentlyContinue"
    $ping=Test-Connection -ComputerName $target
$statusbar1.Text="Pinging $target"
If ($ping) {
    $FQDN = [system.net.dns]::GetHostEntry($target).HostName
If ($Error) {
        $y = $error | select Exception
        $e = $y -split (":")
        $x = $e[2]
        $Error.Clear()
        $statusbar1.Text= $x
} Else {

If ($FQDN){
$IPAddr = [system.net.dns]::GetHostEntry($target).addresslist
foreach ($addr in $IPAddr) {
    $IP = $Addr.ipaddresstostring   
    $rtbPerfData.text="$Target is on Line and `nthe IP is: $IP"
             }
        }

    }
}
    Else {
    $rtbPerfData.text="$target is Off Line"       
    $errorActionPreference="Continue"
    $statusBar1.Text="Ready" }
}

Back in the Designer window, select your ComboBox and in the Properties list, select behavior. Go down to “SelectedIndexChanged and then select GetData in the drop down list on the right.

I added an Exit button to the form as well.

You will note that PrimalForms added a helper function to your script called Load-ComboBox, Leave that alone.

Test your form by running it. The Run File command is in the top center of the Primal Forms Home screen. You can also find it in the Debug Tab. Once it’s working the way you want, Export it to File and a new PS1 file will be created for you. If you want to hand it off to someone else, I would use the Package File option, which creates an EXE that your users “can not” fool with.

You can find some reference to these and other Controls at:

http://www.sapien.com/blog/2011/06/13/primalforms-2011-spotlight-on-the-textbox-control/

Advertisements

Primal Forms Primer III

Not being one to leave well enough alone, I added more bells and whistles to my form.

First, I wanted an Exit button. So I added a button and called it btnExit. I then typed EXIT in the Appearance,Text box. I then added this function to my script:

$btnExit_Click={
$formPingTest.Close()
}

Note that I used the New Name for my form here.

Next, I wanted to work on the output, so I added a RichTextBox from the Toolbox.

I left the name alone and sized it to fit the space.

I then added some code to my btnPing_Click function

$btnPing_Click={
[string]$target=$txtServerName.Text

$ping=Test-Connection -ComputerName $target
$statusbar1.Text="Pinging $target"
If ($ping) {
$FQDN = [system.net.dns]::GetHostEntry($target).HostName
If ($Error) {
$y = $error | select Exception
$e = $y -split (":")
$x = $e[2]
$Error.Clear()
$statusbar1.Text= $x
} Else
{ If ($FQDN){
$IPAddr = [system.net.dns]::GetHostEntry($target).addresslist
foreach ($addr in $IPAddr) {
$IP = $Addr.ipaddresstostring
$richTextBox1.text="FQDN: $FQDN `nIP: $IP"
}
}
Else {
$richTextBox1.text="Server is Off Line"
}
}
}

I added some Error handling and some code to get both the Fully Qualified Domain Name and the IP address of my target server. I then write that out to the RichTextBox like so:

$richTextBox1.text="FQDN: $FQDN `nIP: $IP"

the `n causes the IP address to appear on a new line. If I just wanted a tab between the FQDN and the IP I would have used `t. Note that there is NO Space between the `n and IP: , then I put the whole thing in quotes.

So now in my Status Bar I get the message, “Pinging Server” and in my Rich Text Box I get my results.

Primal Forms Primer II

So now that we have a form with a script, how do we know if it did anything?

I saw something called StatusBar in the tools menu, so I tried that. Turns out it is very simple to use.

Open up your form in PrimalForms and in the Designer tab, select the Toolbox. Way down towards the bottom, you will see “Statusbar”. Drag that over to your form. It will be named Statusbar1. Any time you want to update the Statusbar write $statusbar1.Text= “Some Text”, just as you would Write-Host in your script.

So in my example, I used this for my btnPing action.


$btnPing_Click={
[string]$target=$txtServerName.Text

$ping=Test-Connection -ComputerName $target

If ($ping) {
    $statusbar1.Text="Server Pings"
    }
Else {
        $statusbar1.Text="Server is Off Line"
    }
}

As Test-Connection returns as True or False, I just check the state of $ping and write out a message to the statusbar. I know, Test-Connection will give you a lot more data but that is a whole ‘nother conversation.

I also saw something called the DataGrid that might work. I just have not figured how to use it yet.

You can also rename the form and or the name that shows in the upper left hand corner. Click somewhere in a blank part of your form. The Properties list over on the right has an item called Appearance. In the box called Text, change the word Form to what you want to display up there. I chose “Ping Test”. Down in Design you will see that your form name is now formPingTest. This changes the name in your script as well.

Save your form and you are good to go.

Primal Forms Primer

I usually use HTA’s when I want to create a simple front end for my scripts, but I thought I would give PrimalForms a try for a project I am currently working on.

The first thing I found is that the good folks a Sapien have not gotten around to writing any documentation for this program. So you have to figure things out by looking at samples and guessing.

So , I thought I would write down what I find as I work my way through this to

1. Remind myself in the future

2. Help anybody out who is as clueless as I am.

If some of this is rudimentary or obvious to you, I apologize in advance. If I make any errors or make things more complicated than they need to be, please let me know.

So, the first thing I tried to do was make a form that would set the parameters for a script. BTW, I am using PrimalForms 2011. Seems easy enough. Open up Primal forms go to file new and select New, Form. I selected an Empty Form from the templates.

Next I went to the Toolbox on the left and picked a TextBox and placed it in my form. A new menu pops up on the right where you can set the properties of your new box. Down toward the bottom is Design, Name. Change this name from textbox1 to the name of the parameter you plan on entering. Like txtServerName, later this will become important.

Hopefully you have a working script that you want to use with this form.

Suppose you want to Ping a server.

You could use this Power Shell command to do that:

$ping=Test-Connection -ComputerName $target

So you need to define $target.

[string]$target=$txtServerName.Text #
$ping=Test-Connection -ComputerName $target

So go back to the form and add a Button from the toolbox. In the properties menu, change the name to btnPing in the Design, Name space and type Ping in Appearance, Text space.

On the left side of the form are two tabs. One called Designer and one called script.

Click on Script.

Here is where you paste in your script. Your script becomes a function here so give it a name. As I want it to execute when I press my ping button, I’ll call it btnPing_Click

Like this:

$btnPing_Click={
[string]$target=$txtServerName.Text

$ping=Test-Connection -ComputerName $target

}

Go back to the Designer tab, select your “Ping” button. In the Properties menu,

click the Lightning bolt at the top. In the very first box, Action, Click there will be a drop down box. Your new “function btnPing_Click should be there, select it and we are done for now. Save your form. I called mine TestPing.pff.  You should also take a look at:

Hey, Scripting Guy! How Can I Create a Windows Form Using Windows PowerShell and PrimalForms?

More tomorrow.