PowerShell and Visio Part 1 – Introduction

I’ve been playing around with Visio and PowerShell for quite a while now and the experience is something of a mixed bag.  My first thought was to use PowerShell to build integration diagrams, reading server names, domains, VLANs, and datacenter locations from a database, and adding color-coded connections to show the network connection requirements (e.g. these boxes talk to SQL on these other boxes on port 1433).  While much of the effort was successful, Visio doesn’t have the same idea of “arranging” items on the page.  I figured it would make things not overlap.  It seems to only care if things overlap if they are connected.

I ended up half-automating Visio.  I wrote some functions that did the database lookups and inserted objects (mostly one at a time, but sometimes in sequences), but used it more as an assistant.  I could tell it, for instance, add-server SERVER01-05 and have the script spot the dash, create a range, look up the names of the servers (Server01, for example) and figure out each server’s type (web server, SQL Server, etc.) and IP address.  It would then add an appropriate shape (web server) and label it with the name and IP.

I would share this code, but it’s really tied into the environment I work in and not terribly portable.  Instead, I thought I’d try to build a more general-purpose Visio module and share it as I write it.

My goal is to be able to write a diagram in a PowerShell-based domain specific language (DSL) so that building diagrams will be easier.  They might still need a bit of hand editing, but I’m OK with that.

But what about (insert reference to other PowerShell Visio module here)?  I know there are several well-developed modules out there, but for the most part they didn’t seem to focus on the things that I wanted to or were written in C# (which is fine, but harder to tinker with), and even more so, I wanted to learn how to get PowerShell and Visio to work together.

To give you an idea of what I’m thinking of, here’s some code I just got to work:

Import-Module myvisio -Force
New-VisioApplication                                                                         New-VisioDocument   | out-null                                                              
New-VisioContainer "My new container"  {
    New-VisioRectangle 1 1 5 5 
    New-VisioRectangle 5.5 5.5 6 6
    New-VisioContainer "Inner Container" {
        New-VisioRectangle 1 7 8 8
    } 
} | out-null

The output in Visio looks like this:
visio1

With some appropriate helper functions and some parameters to help with styling, I think this could work.

I’ll share some actual code in the next installment.

Let me know what you think about this.

–Mike