Salutations!

I have been working with Powershell Classes for a long time now. I tend to use them ALL-THE-TIME ! One thing that I realized that would be great to have, is a (UML) diagram of every project that has classes. I think It helps to see the overall solution better, and it eases the technical communication flow between the different engineers working on the solution.

In this post I will show case a Function I wrote called “Write-ClassDiagram“, which is part of my PSClassUtils module,  and it allows to genenrate a (UML-ish) type of class diagram, and will make your coding life, much much easier 😉

For the one that are curious, I have opened sourced the module, and it is available on github here.

 

Installing the Prerequisites:

In order to be able to start and use the solution, you first need to install / or make sure, that you have the following prerequisites:

Powershell version 5.0 (At least)

@kevinMarket PSGraph module, and Graphviz needs to be present as w

To install the prerequisites, simply follow these easy steps:

 

Installing PSClassUtils:

Now that everything is ready, we can go and install PSClassUtils using the following line

By installing and loading the module, we have the following new cmdlets to use:

In this article I’ll cover only  Write-ClassDiagram. I will cover the other cmdlets of this module in seperate blog post.

 

How does it work:

Write-ClassDiagram can generate diagrams of the following cases:

  • powershell classes (only) that are located in:
    • a single PS1 file
    • a single psm1 file
    • A folder structure, containing one or more ps1 or psm1 file.

Each of the file (ps1/psm1) can contain one or more powershell classes.

Hiearchy that crosses several files, is also supported.

Files containing Csharp classes with Add-Type, are not supported.

One thing that is important to notice, is that in the current version, only classes located in files are supported. Creating diagrams from Classes located only in memory, is something that is currently not supported.

 

A concrete Example:

 

Let’s say we have the following (very basic) class located, in C:\temp\Inheritance.ps1

 

 

Calling Write-ClassDiagram

To generate the diagram, we will use the command as follow.

 

Using the -Show parameter will  open the generated document immediatly.

 

In our case, will generate the following output:

I went ahead, and ran the Write-ClassDiagram on all the projects I could think of. Not all of mine are actually opened sourced.

 

Some examples from the holy Internet:

I have asked a bit around for cool projects using powershell classes, and went ahead and started to generate Diagrams for each of these projects.

 

Class.HostsManagement by Stéphane van Gulick (me ;))

 

StarShip Module from Jeff Hicks

 

But the most impressive Diagram so far, is the one I generated for the PoshBot project from Brandon Olin


I’ll invite you to download a PSClassUtils, and try it out. Looking forward to your feedback!

 

#Stéphane

By | 2018-06-30T09:33:56+00:00 June 15th, 2018|PowerShell|2 Comments

About the Author:

Stéphane is a dynamic and passionate Cloud and datacenter Microsoft MVP since. He is the founder of the Basel PowerShell user Group (BPUG), the co-founder of the French Speaking PowerShell UserGroup (FRPSUG), author, blogger, and received the community award "PowerShell Hero" from PowerShell.org. Stéphane has implemented microsoft infrastructure solutions in various countries of Europe and is currently working in Basel / Switzerland. Stéphane help his clients to reduce their global infrastructure costs by implementing Microsft infrastructure solutions by combining great products such as System Center, Windows Server, with heavy automation using Windows PowerShell. Stéphane loves languages, Belgium beer, French cheese and French Wine. If any of these topics are of your interest, don't hesitate to come and say hi.

2 Comments

  1. Jonas Andersson November 6, 2018 at 5:21 pm - Reply

    What about PowerShell functions?

    • Stephane November 6, 2018 at 5:23 pm - Reply

      This is currently not supported. how would you imagine this implemented? Do you have a use case for it? If yes, which one.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: