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. 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:tempInheritance.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