PowerShell In GUI Blog

PowerShell GUI Box Just In A Few Clicks

Using Quest cmdlets from .NET. The complete walkthrough. Part 2

with one comment

A pair of weeks ago I published a jocular application called ‘QAD in GUI’. Today I’m going to add a bit more controls to that app so as not to feel that I did something useless.

First of all, what does the end user expect? Not surprisingly, that something alike output in a command line console, but represented graphically. This is the first requirement saying that our output should be the same as from command line, but represented in the GUI. As output is usually an array of objects or strings, there might be used a list box, a combo box, a list view and similar controls providing textual information line by line.

Second, the detail view is necessary. The best control doing that is a property grid. Since we could have several results, it’s a good idea to store them in the memory and fill the property grid with a portion of data the user wants.

Of course, scripts in the file system should be supported as well as typed code. WinForms are also to be supported.

Moving to the end of requirements list, what’s about objects? Many of them have hierarchy below and this is also a matter of our interest. There are several controls that might help us, let’s a tree view to be our choice.

The code sample provided below is a typical example of using Windows.Forms so that it’s no need to explain more than done in the comments (however, you may ask for additional info if you need).

Imports System.Management.Automation

Imports System.Management.Automation.Runspaces

 

Public Partial Class MainForm

       

       Public Sub New()

             ' The Me.InitializeComponent call is
required for Windows Forms designer support.

             Me.InitializeComponent()

             '

             ' TODO : Add constructor code after InitializeComponents

             '

       End Sub

       

       Sub
Button1Click(sender As Object, e As EventArgs)

             runCommand

       End Sub

       

       Sub
runCommand

       'Our commandto run

       Dim strCode As String

             'strCode = Me.textBox1.Text

             strCode = Me.richTextBox_Input.Text

       Me.statusStrip_Info.Text =
"Running"

       'Create runspace condiguration to
add QAD snapin

       Dim conf As RunspaceConfiguration =
RunspaceConfiguration.Create()

       Dim warning As PSSnapInException = Nothing

       Dim
info As PSSnapInInfo 

       info
= conf.AddPSSnapIn("Quest.ActiveRoles.ADManagement", warning)

       'A new runspace object

       Dim runspace As Runspace =
RunspaceFactory.CreateRunspace(conf)

             runspace.Open

       'A pipeline

       Dim pipeline As Pipeline =
runspace.CreatePipeline(strCode)

       'Collection for results

       Dim results As System.Collections.ObjectModel.Collection(Of PSObject)

       '

       Try

             'Clean-Up

             Me.comboBox_Output.Items.Clear()

             Me.listBox_Output.Items.Clear()

             Me.listView_Output.Items.Clear()

             If (Me.listView_Output.Columns.Count
= 0) Then

                    Me.listView_Output.Columns.Add("Results")

             End If

             Me.treeView_Output.Nodes.Clear()

             Dim node As TreeNode = Me.treeView_Output.Nodes.Add("Results")

             Me.propertyGrid_Output.SelectedObject = Nothing

             'Run pipeline

             results = pipeline.Invoke()

             'If results are available

             'obtain
them and put to the combo box, the list box,

             'the
tree view, and the list view

             If (results.Count > 0) Then

                    For
Each psObj In results

                           'Add to the combo box

                           Me.comboBox_Output.Items.Add(psObj)

                           'Add to the list box

                           Me.listBox_Output.Items.Add(psObj)

                           'Add to the list view

                           Dim newItem As ListViewItem = _

                                  Me.listView_Output.Items.Add(psObj.ToString())

                           newItem.Tag
= psObj

                           'Add to the tree view

                           Dim newNode As TreeNode = _

                                  Me.treeView_Output.Nodes(0).Nodes.Add(psObj.ToString())

                           newNode.Tag
= psObj

                           'Add hierarchy if available

                           For Each member In psObj.Members

                                  newNode.Nodes.Add(member.ToString())

                           Next

                    Next

             End
If

              Me.listView_Output.AutoResizeColumns(System.Windows.Forms.ColumnHeaderAutoResizeStyle.HeaderSize)

             node.Expand()

             'Set the first item as text in
the combo box

             If (Me.comboBox_Output.Items.Count
> 0) Then

                    Me.comboBox_Output.SelectedIndex =
0

             End If

             'Indicate that processing of
results is finished

             Me.statusStrip_Info.Text = "Ready"

       Catch excp as Exception

             MessageBox.Show("Error:"
& excp.Message)

       End Try

       

       End
Sub

       

       Sub
MainFormPaint(sender As Object, e As PaintEventArgs)

             Me.splitContainer_GUI.Height = Me.Height - 2 - Me.statusStrip_Info.Height

             Me.richTextBox_Input.Width = Me.Width - 100

       End Sub

       

       Sub
ComboBox_OutputSelectedIndexChanged(sender
As Object, e As EventArgs)

             Me.propertyGrid_Output.SelectedObject = Me.comboBox_Output.SelectedItem

       End Sub

       

       Sub
ListBox_OutputSelectedIndexChanged(sender
As Object, e As EventArgs)

             Me.propertyGrid_Output.SelectedObject = Me.listBox_Output.SelectedItem

       End Sub

       

       Sub
TreeView_OutputAfterSelect(sender
As Object, e As TreeViewEventArgs)

             Me.propertyGrid_Output.SelectedObject = Me.treeView_Output.SelectedNode.Tag

       End Sub

       

       Sub
ListView_OutputSelectedIndexChanged(sender
As Object, e As EventArgs)

             Try

                    Me.propertyGrid_Output.SelectedObject
= Me.listView_Output.SelectedItems(0).Tag

             Catch

             End Try

       End Sub

End Class

This sample is also available in the 'My Shared Files' box as 'QADinGUI'.
Advertisements

Written by Alexander Petrovskiy

July 19, 2010 at 4:40 pm

Posted in .NET, Powershell, Quest

Tagged with ,

One Response

Subscribe to comments with RSS.

  1. […] QADinGUI.zip relating to both Using Quest cmdlets from .NET. The complete walkthrough and Using Quest cmdlets from .NET. The complete walkthrough. Part 2 […]


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

%d bloggers like this: