Build GUI for PowerShell

PowerShell

Written by:

Hey everyone! Today I want to talk about GUI for PowerShell. You probably wonder why? PowerShell is all about the CLI and moving away from the GUI. but sometimes, a GUI for our scripts can be very useful.

Every Company has HelpDesk team, other System Admins that don’t know PowerShell at all, or non-technical staff. While I will encourage them to learn, I can’t make them. As a result, I need to provide them with a GUI for some of the tools or solutions.

Old lady with computer meme asking for PowerShell GUI


First of all, let’s talk about PowerShell GUI. In fact, There is no such thing “PowerShell GUI” in the PowerShell world, The GUI part is part of the .NET world (which PowerShell built upon). There are Two main ways to provide GUI for our scripts and both of them related to .NET:

Windows Presentation Foundation – WPF

WPF is a subsystem of the .NET framework and can be used for rendering user interfaces in Windows-based applications. Due to the dependency of .NET, WPF is supported only in Windows-based clients.

Windows Forms – WinForms

Similarity to WPF, WinForms is also part of the .NET framework. WinForms is a graphical (GUI) class library included as a part of the Microsoft .NET Framework or Mono Framework, and also supported only in Windows-based clients.

Soon with .NET Core, we will see solutions for Linux GUI also šŸ˜ƒ

The task

In my company, we use Service Accounts, those accounts are used to run Business Application Services, Tasks and more. They can also be used for developers teams, needing the account for the systems tests run and so.
In order to create a Service Account, one shall follow the following guidelines:

  • SamAccountName must start with: Srvc
  • Must have a manager (employee who takes responsibility for the account if any anomaly or issue detected)
  • Account must have the following description “Service Account for <Account purpose>”
  • Account office attribute: Must include “Managed by <Employee name> (<Employee ID>)

Seems like an easy task right? but when you have a team that needs to do it, a few times a week, it becomes error prone quite fast. So how can we provide a solution?

The GUI side

I want to provide the team with a tool that will get the information, and perform the creation automatically. In addition, I want to send emails for confirmations and audit.
First, I like to start with the GUI design, it will make it easier to know what functions I need to build in my script. For small tasks like this, I love to use
POSHGUI, it’s a web tool that let you build Winforms GUI for PowerShell in minutes.

My Service Account Creator Tool GUI

I won’t include the “Office” attribute as I have all the information needed to update it myself.

GUI code overview

First, we need to Add-Type, this cmdlet adds a .NET class to our Powershell Session. Then we enable visual styles to our form, this adds theme style.

Global configuration

Secondly, we need to define global properties, including the form itself, the size of the window, title and more:

The Service Account Name

This ‘TextBox’ will be the SamAccountName of the user, as I said before, it must start with Srvc so I typed it in by default as a reminder.

Manager Employee ID

In our company the Employee ID is the unique identifier in the Active Directory, we will use it to find the correct employee. we will use it to fill in the information in the service account and to send him a confirmation mail.

Creator Employee ID

Same as the Manager Employee ID, this time for the creator employee ID, we will use this to audit the process and send him a confirmation mail.

Description

What is the purpose of this Service account, same as service account name, it must start with “Service Account for ”

Create Account button

Every program needs an action button. Here we need only one button.

Status Bar
Create the form

After we specify each component in our form, we need to invoke it.

Add events

After we have the form, we need to add an event, there are plenty events to execute and you can explore them in POSHGUI, here in our example we need to add click action to our button, we will put there the code we want to execute each time we press the button.

wrapping up

Finally, we need to close the GUI section and invoke the ShowDialog() method to display our form

The PowerShell side

The GUI we have is nice, but it doing nothing. In order for the GUI to do something when we click on the button “Create Account” we need to add our PowerShell script in the button click action. But before we do it we need to write some helper functions for our script:

Get Employee by employeeID

The first function will help us get the information we need for the Manager and Creator employees, it will look for the employee ID in the Active Directory and return the user if found, or $false if not.

Generate a Password

Then we need to be able to generate passwords, to do that I’m using my New Password Generator core function.

Create Mail Reports

Another task we need to perform is to send emails with the account information. I’m creating two emails, but it’s up to you to decide. I create one mail for the user creator and the user manager, with the account info and password. Second mail is for the security team audit, simple mail that state that account has been created, with all the information except the password.

Create new Service account

Last but not least, the reason we are here, the Service account creation function. In this function, We specify the account creation OU, the password and the rest of the attribute needed.

The Script

We built the GUI and the functions needed to perform the creation task. If you remember, when we created the GUI, we add an Action event. The script we build now is the script we want to execute each time someone presses the key.

Content validation

We will start with content validation, to make sure that the Help Desk operator is following the Service Account creation guideline.

Creating the Service Account

Now after we finished with the validations, we can create the Service Account:

Conclusion

We learned how to build GUI for our PowerShell scripts. How it can help us help others who don’t know yet PowerShell. And even better, give them the motivation to learn PowerShell and maybe even create their own GUI šŸ˜‰
I would love to hear your GUI’s Ideas!
You can see the complete script here.

Comments are closed.