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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s