PowerShell In GUI Blog

PowerShell GUI Box Just In A Few Clicks

Posts Tagged ‘.NET sample

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 ,

Using Quest cmdlets from .NET. The complete walkthrough

with one comment

A question had been raised on the possibility of using cmdlet in the VB.NET GUI application. The example below demonstrates how to use cmdlets as .NET classes.

First of all, go to the page http://www.quest.com/powershell/activeroles-server.aspx and download the latest QAD cmdlets.

In case you don’t have an IDE where you are planning to test this example, go to the page http://icsharpcode.com/OpenSource/SD/Download/and download SharpDevelop 2 or 3. Alternatively, you might use Visual Studio Express available here http://www.microsoft.com/express/Downloads/ or any professional edition.

Next, install all the downloaded. I’ll be demonstrating on the Windows 2003 SP2.

On opening an IDE, I’ll be using SharpDevelop 3.2 for this purpose, as do I do almost always, you need to create a solution.

Now, we need discover which libraries do we need to register. The following snippet of code, being run in, for example, PowerGUI ScriptEditor, helps us:

cls
[System.Reflection.Assembly[]]$asmArray =
    [System.AppDomain]::CurrentDomain.GetAssemblies();
foreach($assembly in $asmArray)
{
    if ($assembly.Location.Length -gt 0 -and `
        $assembly.Location.Contains('Quest'))
    {
        Write-Host $assembly.Location
    }
}
The output recommends us to use the following libraries: 

C:\Program Files\Quest Software\Management Shell for AD\Quest.ActiveRoles.ArsPowerShellSnapIn.dll C:\Program Files\Quest Software\Management Shell for AD\Quest.ActiveRoles.ArsPowerShellSnapIn.DirectoryAccess.dll C:\Program Files\Quest Software\Management Shell for AD\Quest.ActiveRolesServer.Common.dll
Also we must add System.Management.Automation.
Note: it's not common to use commandlets directly from their assemblies, so that for the purpose of simplicity we'll be using System.Management.Automation only. However, you might find it useful to browse these assemblies in the Object Browser in an IDE of your choice.
After that, we need to add the two following using, I beg your pardon, Imports, statements:
Imports System.Management.Automation
Imports System.Management.Automation.Runspaces
Next, let's add controls to our form: a textbox, a button and a propertygrid:
Add to the Click event the following code:
Sub Button1Click(sender As Object, e As EventArgs)           
    runCommand        
End Sub         
      
And create the subroutine:
 Sub runCommand        
   'Our commandto run        
   Dim strCode As String              
     strCode = Me.textBox1.Text        
   '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              
     results = pipeline.Invoke()              
    Me.propertyGrid1.SelectedObject = results(0)        
   Catch excp as Exception              
    MessageBox.Show("Error:" & excp.Message)        
  End Try              
End Sub
At last, let's run the solution, type Powershell code and enjoy seeing results in the property grid control:
Source code can be found here:
and in My Shared Files at right (QADinGUI.zip). NB: This post is partly based on the guide: 
http://p2p.wrox.com/book-professional-windows-powershell-programming-isbn-978-0-470-17393-0-386/

Written by Alexander Petrovskiy

July 7, 2010 at 9:15 pm

Posted in .NET, Powershell, Quest

Tagged with ,