PowerShell In GUI Blog

PowerShell GUI Box Just In A Few Clicks

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/
Advertisements

Written by Alexander Petrovskiy

July 7, 2010 at 9:15 pm

Posted in .NET, Powershell, Quest

Tagged with ,

One Response

Subscribe to comments with RSS.

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


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: