DEV Community

absterellio
absterellio

Posted on

Adding a simple (old-fashioned) GUI to Azure Devops CLI script

Using WinForms!

I wrote a script to pull information from my codebase (see: Using Azure Devops CLI to generate repository reports. I updated the script to present a (very simple) interface to the user to ease usage and provide a way to use the tool without having to deal with command line inputs.

The updated script takes in an input file path, an output file path and personal access token to access the Azure CLI. For more information on the Azure Devops CLI, see the Azure DevOps CLI documentation or my previous article.

The Result

Repo Report interface

The Code

The script can be found on my GitHub page:

GitHub logo absterellio / azure-devops-cli-tool-v2

azure-devops-cli-tool-v1 with a GUI added

azure-devops-cli-tool-v2

This project adds a basic GUI to the azure-devops-cli-tool-v1 project found here: https://github.com/absterellio/azure-devops-cli-tool-v1.

NOTE: In the azure-repo-script.ps1 file, replace any variables that begin with "ex-" with your own applicable Azure devops values in order for this script to run.




For this script, I added a simple user interface using Windows Forms. I've never used Windows Forms to build a GUI before so I wanted to take note of how I made use of it and some things I learned along the way. This article will walk through the very basics of adding some input controls and logic to the script.

Getting started with Windows Forms

First, in order to use Windows Forms, I added these line to the top of my file.

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

I built an intial landing screen that will pop up when the script is first run. The following code creates a basic pop-up form with the dimensions 600 x 400 pixels in the middle of the screen view area.

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Repo Report'
$form.Size = New-Object System.Drawing.Size(600,400)
$form.StartPosition = 'CenterScreen'

Adding inputs and text fields:

New inputs can be added as follows. To build a label, I added a Label control, specifying a start point, size, and text value for the label.

$label1 = New-Object System.Windows.Forms.Label
$label1.Location = New-Object System.Drawing.Point(150,70)
$label1.Size = New-Object System.Drawing.Size(280,40)
$label1.Text = 'Enter a text file to read from. (Note: Specify each repo you would like to query on a new line of the file):'
$form.Controls.Add($label1)

To build an input field, I added a TextBox, specifying a start point and size for the input.

$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(150,110)
$textBox1.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox1)

I added a cancel button with the Button control. In addition to specifying dimensions and size, I added a DiaglogResult property, which will help to handle actions when this button is clicked.

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(200,200)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

Handling events:

After creating all of the inputs, I showed the dialog by specifying:

$result = $form.ShowDialog()

Now, '$result' will hold the next button press or event thrown from the UI. We can now handle any event, such as a button press, within an 'if' check for that control.

if ($result -eq [System.Windows.Forms.DialogResult]::OK)

Any logic that follows can live here.


I hope this article gave some insight into getting started with WinForms and adding a very simple interface to a PowerShell script. Input or suggestions always welcome.

Thanks for reading!

Top comments (0)