For a while now, I’ve been demonstrating Visio automation in PowerShell. For the most part, the posts have included a bunch of short code snippets designed to illustrate simple concepts.

Unfortunately, the code has been filled with “magic” constants, and lots of method calls on the various Visio COM objects. Both of those make code hard to read and write, the exact opposite of what you want as a PowerShell scripter. What’s the solution? PowerShell makes it easy to wrap complex code in cmdlets (or advanced functions, if you prefer) and expose a much simpler interface.

Background

When I started looking at driving Visio from PowerShell, I ran across the VisioAutomation project (originally here) by Saveen Reddy. Contained in that project (which is much broader in scope than my interests) was a module called VisioPS that exposed several Visio concepts as PowerShell cmdlets. I played with it some, and it worked. That was exciting. A major downside for me was that the module was a binary module, which meant writing cmdlets in C#. That’s a great approach for the project, which is geared towards .NET languages in general, but it limits the attractiveness for PowerShell developers (or at least for me). I ended up re-writing parts of the module in PowerShell and decided eventually to write my own module. There are probably some naming artifacts left over from its origin as a “re-implementation” of VisioPS. I wanted to make sure to give credit for getting me started down this road.

What is VisioBot3000?

To start with, VisioBot3000 implements cmdlets to help you draw Visio diagrams using the things that I’ve introduced over the last several posts:

Documents

Pages

Stencils

Masters

Shapes

Containers

Connectors

It also has some support (barely) for Layers.

Here’s a simple (annotated) example of using VisioBot3000. It uses a custom stencil that I have on my system (and a stock stencil that I copied to c:\temp), but that’s not terribly important:

Import-Module VisioBot3000 -Force #start Visio and create a new document New-VisioApplication New-VisioDocument C:\temp\TestVisioPrimitives.vsdx #tell Visio what Stencils I want to use and give them "nicknames" Register-VisioStencil -Name Containers -Path C:\temp\MyContainers.vssx Register-VisioStencil -Name Servers -Path C:\temp\SERVER_U.vssx #pick a master from one of those stencils and give it a nickname Register-VisioShape -Name WebServer -From Servers -MasterName 'Web Server' Register-VisioShape -Name DBServer -From Servers -MasterName 'Database Server' #pick another master (this time a container) and give it a nickname #note that this is a different cmdlet Register-VisioContainer -Name Domain -From Containers -MasterName 'Domain' #draw a container with two items in it New-VisioContainer -shape (get-visioshape Domain) -name MyDomain -contents { New-VisioShape -master WebServer -name PrimaryServer -x 5 -y 5 New-VisioShape -master DBServer -Name SQL01 -x 5 -y 7 } #add a connector New-VisioConnector -from PrimaryServer -to SQL01 -name SQL -color Red -Arrow

That probably seems like a lot of code, but it’s a lot less than you’d have to write to draw the diagram. A lot of it is boilerplate code that you’d probably factor out into a “standard diagram stencils and masters script”. And look….the only “magic numbers” in the code are the x/y positions. Not super bad, in my opinion.

Here’s the output:



This code is a huge (yuge?) improvement over writing code against the bare COM objects. But it’s not even close to the best part of VisioBot3000.

Did you notice that the title said “Clark Kent”? Stay tuned until next time when VisioBot3000 takes of its glasses, so to speak, and the power is really revealed.

As usual, let me know what you think in the comments (or on Reddit, or Google+, or Twitter)

–Mike

P.S. VisioBot3000 lives on github here.