How to empower GUI Automation and integrate it in Powershell

author

Aziro Marketing

Oct 24 - 0 min read

How to empower GUI Automation and integrate it in Powershell
play-pause-btn

Audio : Listen to This Blog.

In this blog we will discuss about how to empower GUI Automation and integrate it into Powershell framework. In general, Powershell scripts are mainly used for automating the middleware and backend system functionality.

But, in order to get end-to-end automation, we will have to integrate the frontend GUI Automation too. GUI Automation can be used as a validation point, soon after performing the middleware and backend system API execution.

This can be achieved by using binary module [Using C# .Net Automation Libraries] through which UI automation can be bridged with middleware and backend system automation.
Let’s take a look at ā€œHow binary module integration is doneā€

What is Powershell Binary Module?

Importing a dll [Using C# .Net libraries] into powershell is referred to as Binary Module. A binary module can be any assembly (.dll) that contains cmdlet classes. A dll which contains cmdlet classes can be imported into powershell. By default, all the cmdlets in the assembly are imported when the binary module is imported.
Binary Module integration is segregated into two divisions,

  1. Creating library file using C#.
  2. Loading the dll into powershell

1. Creating library file using C#.

STEP 1 : Creating a New Project

Select the Visual C# from the installed Templates list and then pick Class Library since compiled modules in PowerShell are in DLL format. Let’s name this project as DemoModule.

Empowering-GUI-1.jpg

STEP 2 : Navigate to Properties

Select the Debug option in the menu and select DemoModule Properties.

Empowering-GUI-2.jpg

STEP 3 : Choose .NET framework

Go to Application and choose the appropriate (suitable for powershell version) .NET framework version under ā€œTarget frameworkā€.

Empowering-GUI-3.jpg

In Visual Studio 2012 the assembly will be built using .NET 4.5, but you’ll need to switch it to use the older framework, so that Powershell can load the assembly.
Note : Select the .NET Framework depending on the lowest version of PowerShell to support. You can use the following as reference:
PowerShell 2.0 – .NET Framework 3.5
PowerShell 3.0 – .NET Framework 4
PowerShell 4.0 – .NET Framework 4.5

STEP 4: Adding reference.

Right click on Reference in the Solution Explorer and select Add Reference.

Empowering-GUI-4.jpg

Loading the following UI Automation library as reference

Empowering-GUI-1.jpg

Need to load the System.Management.Automation library as a reference to our project to make the PowerShell API calls available.

Select browse under Reference manager.

Navigate to location
C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35 and select the file System.Management.Automation.dll

Empowering-GUI-6.jpg

After adding the following library in references.

Empowering-GUI-7.jpg

STEP 5 : Import ā€œSystem.Windows.Automationā€ library

Include the ā€œSystem.Windows.Automationā€ reference to the class to get access to API calls in order to build a PowerShell module.

Empowering-GUI-8.jpg

STEP 6 : Converting the class into a single Powershell cmdlet

Creating Powershell cmdlet using dataanotation. ā€œ[System.Management.Automation.Cmdlet(System.Management.Automation.VerbsCommon.Get, ā€DemoNameā€)]ā€

Empowering-GUI-9.jpg

STEP 7 : Build the Project to dll

Build solution is done either by hitting F7 or selecting ā€œBuild Solutionā€ from the BUILD menu. After build succeeds, collect the dll from the path displayed in Output pane.

Empowering-GUI-10.jpg

2. Loading the dll into powershell :

STEP 1: Navigate the location of dll with Import-Module along with -Verbose to load the module and give output. It shows that Get-DemoName is loaded.
DLL received from .Net environment is been loaded into Windows powershell prompt using cmdlet ā€œImport-Moduleā€

STEP 2: Get-Module displays the module information which in turn implies that DLL gets loaded successfully.

Empowering-GUI-11.jpg

Powershell cmdlet ā€œGet-help Get-DemoNameā€ shows the detailed information of the cmdlet

Conclusion

Importing binary modules written in C# helps achieve GUI automation through Powershell.

How to empower GUI Automation and integrate it in PowershellHow to empower GUI Automation and integrate it in PowershellHow to empower GUI Automation and integrate it in Powershell

LET'S ENGINEER

Your Next Product Breakthrough

Book a Free 30-minute Meeting with our technology experts.

Aziro has been a true engineering partner in our digital transformation journey. Their AI-native approach and deep technical expertise helped us modernize our infrastructure and accelerate product delivery without compromising quality. The collaboration has been seamless, efficient, and outcome-driven.

Customer Placeholder
CTO

Fortune 500 company