PowerShell and Visio

I’ve blogged about using PowerShell with various members of the Office family before. I thought its about time I looked at Visio.  I use Visio quite a bit and it would be useful to be able to drive it with PowerShell

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
## create visio document
$visio = New-Object -ComObject Visio.Application
$docs = $visio.Documents

## use basic template
$doc = $docs.Add("Basic Diagram.vst")

## set active page
$pages = $visio.ActiveDocument.Pages
$page = $pages.Item(1)

## Add a stencil
$mysten = "C:\Program Files\Microsoft Office\Office14\Visio Content\1033\EntApp_M.vss"
$stencil = $visio.Documents.Add($mysten)

$server = $stencil.Masters.Item("Server")
$workstn = $stencil.Masters.Item("Workstation")

$shape1 = $page.Drop($server, 2, 2)
$shape2 = $page.Drop($server, 5, 5)

$doc.SaveAs("c:\scripts\visio\draw1.vsd")
$visio.Quit()

Office still uses COM objects so we need to create a Viso object and then the document and page we are working on.  We are using the basic template and are going to add a stencil for the Enterprise Applications. A couple of objects are created for the server and workstation shapes and then we drop them on to the page.

We can then save the drawing and quit visio.

A simple drawing that we will need to embellish but it shows how straight forward it is to automate the production of drawings. The great thing is that we can then go back into the drawing and manipulate it as we need.

Leave a Reply