• Stars
    star
    191
  • Rank 202,877 (Top 4 %)
  • Language
    PowerShell
  • License
    MIT License
  • Created almost 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A set of utilities for working with Graphviz in Powershell

Build status Documentation Status

PSGraph

PSGraph is a helper module implemented as a DSL (Domain Specific Language) for generating GraphViz graphs. The goal is to make it easier to generate graphs using Powershell. The DSL adds these commands that are explained below.

What is GraphViz?

Graphviz is open source graph visualization software. Graph visualization is a way of representing structural information as diagrams of abstract graphs and networks. It has important applications in networking, bioinformatics,  software engineering, database and web design, machine learning, and in visual interfaces for other technical domains. 

Project status?

Beta release. The core module work and documentation is fleshed out. The simple features are implemented but there are other features of the DOT language that I have not used much myself. The command names and arguments of the existing commands should be stable now. As always, more testing still needs to be done.

GraphViz and the Dot format basics

The nice thing about GraphViz is that you can define nodes and edges with a simple text file in the Dot format. The GraphViz engine handles the layout, edge routing, rendering and creates an image for you.

Here is a sample Dot file.

digraph g {
    "Start"->"Middle"
    "Middle"->"End"
}

This will produce a graph with 3 nodes with the names Start, Middle and End. There will be an edge line connecting them in order. Go checkout the GraphViz Gallery to see some examples of what you can do with it.

How PSGraph can help

we can create those Dot files with PSGraph in Powershell. Here is that same graph as above.

digraph g {
    edge Start Middle
    edge Middle End
}

Here is a second way to approach it now.

$objects = @("Start","Middle","End")
digraph g {
    edge $objects
}

The real value here is that I can specify a collection to process. This allows the graph to be data driven.

PSGraph Commands

I tried to keep a syntax that was similar to GraphViz but offer the flexibility of Powershell. If you have worked with GraphViz before, then you should feel right at home.

Graph or digraph

This is the container that holds graph elements. Every valid GraphViz graph has one. digraph is just an alias of graph so I am going to use the shorter graph for the rest of the readme.

graph g {        
}

Edge

This defines an edge or connection between nodes. This command accepts a list of strings or an array of strings. It will connect them all in sequence. This should be placed inside a graph or subgraph. -From and -To parameters are available if you want more verbosity.

graph g {
    edge -From first -To Second
    edge one two three four
    edge (1..5)
}

If you supply two arrays, it will cross multiply them. So each item on the left with have a path to each item on the right.

graph g {
    edge (1,3) (2,4)
}

Because this is Powershell, we can mix in normal commands. Take this example.

$folders = Get-ChildItem -Directory -Recurse
graph g {
    $folders | %{edge $_.Name $_.Parent} 
}

I also support edge attributes that are defined in the DOT specification by using a hashtable.

graph g { edge "Point One" "Point Two" @{label='A line'} }

Node

The Node command allows you to introduce a node on the graph before an edge is created. This is used to give specific nodes different attributes or to place them in subgraphs. The node command will also accept an array of values and a hashtable of attributes.

graph g {
    node start @{shape='house'}
    node end @{shape='invhouse'}
    edge start,middle,end
}

This is the exact Dot output generated those node commands.

digraph g {

    "start" [shape="house"]
    "end" [shape="invhouse"]
    "start"->"middle" 
    "middle"->"end" 
}

Rank

The rank command will place the specified nodes at the same level in the chart. The layout can get a bit wild sometime and gives you some control.

graph g {
    rank 2 4 6 8
    rank 1 3 5 7
    edge (1..8)
}

SubGraph

This lets you box of and cluster nodes together in the graph. These can be nested. The catch is that you need to number them starting at 0. Attributes are supported with the -Attributes parameter.

graph g {
    subgraph 0 -Attributes @{label='DMZ'} {
        node Web1,Web2
    }
    edge web1 database
    edge web1 database
}

More complex example

We can pull that all together and generate quite the data driven driven diagram.

$webServers = 'Web1','Web2','web3'
$apiServers = 'api1','api2'
$databaseServers = 'db1'

graph site1 {
    # External/DMZ nodes
    subgraph 0 -Attributes @{label='DMZ'} {
        node 'loadbalancer' @{shape='house'}
        rank $webServers
        node $webServers @{shape='rect'}
        edge 'loadbalancer' $webServers
    }

    subgraph 1 -Attributes @{label='Internal'} {
        # Internal API servers
        rank $apiServers
        node $apiServers   
        edge $webServers -to $apiServers
    
        # Database Servers
        rank $databaseServers
        node $databaseServers @{shape='octagon'}
        edge $apiServers -to $databaseServers
    }    
}

Installing PSGraph

Make sure you are running Powershell 5.0 (WMF 5.0). I don't know that it is a hard requirement at the moment but I plan on using 5.0 features.

# Install GraphViz from the Chocolatey repo
Register-PackageSource -Name Chocolatey -ProviderName Chocolatey -Location http://chocolatey.org/api/v2/
Find-Package graphviz | Install-Package -ForceBootstrap

# Install PSGraph from the Powershell Gallery
Find-Module PSGraph | Install-Module

# Import Module
Import-Module PSGraph

For OSX, you can use brew to install graphviz.

brew install graphviz

Generating a graph image

I am still working out the workflow for this, but for now just do this.

# Create graph in variable
$dot = graph g {
    edge hello world
}

# Save to file
Set-Content -Path $env:temp\hello.vz -Value $dot

Export-PSGraph -Source $env:temp\hello.vz -Destination $env:temp\hello.png -ShowGraph

The export can be done more in line if needed.

graph g {
    edge hello world
} | Export-PSGraph -ShowGraph

More Repositories

1

Chronometer

A module for measuring performance of Powershell scripts, one line at a time
PowerShell
75
star
2

PesterInAction

These are the demo files for my presentation on Pester.
PowerShell
41
star
3

PlasterTemplates

A set of Plaster templates that I use.
PowerShell
38
star
4

PSGraphPlus

A utility module that makes use of PSGraph
PowerShell
29
star
5

PowershellWorkspace

My Powershell scripts and modules
PowerShell
23
star
6

jira

PowerShell module for Jira
PowerShell
15
star
7

PSHonolulu

PowerShell module for working with Project Honolulu
PowerShell
11
star
8

KMT.ModuleBuilder

Kevin Marquette Tools: Powershell module for building modules
PowerShell
11
star
9

HashtablePresentation

Presentation given to PowerShell user groups
PowerShell
9
star
10

Select-Ast

A PowerShell module for working with the AST
PowerShell
9
star
11

ConvertTo-Breakpoint

PowerShell function for converting an error record to a breakpoint
PowerShell
9
star
12

PowerShell-Lambda-Runtime

A custom PowerShell Lambda runtime that captures the spirit of PowerShell.
PowerShell
8
star
13

kevinmarquette.github.io

Kevin Marquette's website and blog
HTML
7
star
14

Watch-Command

Watch-Command will continuously execute a command for monitoring results
PowerShell
7
star
15

kmt.winget.autocomplete

Adds autocomplete in Powershell for winget
PowerShell
6
star
16

ManagingModulesPresentation

Managing PowerShell Modules and Repositories Presentation
PowerShell
6
star
17

PSGraphPresentation

Presentation materials for PSGraph
PowerShell
6
star
18

FindObsolete

A function to find obsolete parameters in scripts
PowerShell
5
star
19

ClassesPresentation

User group presentation on PowerShell classes
PowerShell
4
star
20

Format-Splat

Adds PowerShell splat formatting support to VSCode
PowerShell
4
star
21

AdvancedFunctionPresentation

Powershell Presentation on Advanced Functions
PowerShell
4
star
22

DSLPresentation

Writing a DSL (Domain Specific Language) for PowerShell
PowerShell
3
star
23

ModulePresentation

Presentation on PowerShell Modules
PowerShell
2
star
24

PSTypeExtension

Example implementation of the DSL specified in PowerShell RFC0017
2
star
25

KMT.ProjectStarterKit

PowerShell module for creating PowerShell projects
PowerShell
2
star
26

CommonAdminPresentation

Powershell: General admin problems and how to solve them
PowerShell
1
star
27

CTSMUG

PowerShell
1
star
28

PowerShell-Core-Compatibility-Alias

Adds aliases to PowerShell Core that were removed from Windows PowerShell
PowerShell
1
star
29

PowerShellSocial

PowerShell scripts and modules from my livestreams
PowerShell
1
star
30

Get-RandomCommand

PowerShell
1
star