Every good blog post should have some real life story to help you visualise why things happen so here goes:
The Story
One day whilst sat in the office updating the disaster recovery documentation I needed to document the Virtual Infrastructure, we all know that managers like pretty pictures so the first thing they asked for was a pretty diagram of how the infrastructure looked.
After dropping my 40
th VM object on the page I started to get a bit bored, i looked at other options. There is already a great tool on the market that performs this job,
Veeam Reporter produces multiple documents and does a great job, but being a contractor I have a n
il budget. Knowing that you can do some very cool things with
Powershell I wondered how easy it would be to hook into the
Visio com object to get
powershell to create the document for me.
As it turns out it
wasn’t that hard, there were a few sticky moments where I had to refer to the
Visio forums, and a great starting point by the only powershell reference using Visio I could find (Check out
CommandBreak_ Thanks Joeseph) after explaining what
powershell was to the guys in the Visio Forum and explaining why I was using
powershell as I think this is the first time these people had heard of it, I started to relate some of there c# examples back into
powershell.
The Result
The result is by no means a totally polished all singing all dancing documenter that documents your VI from the left, from the right and upside down but what it is, is an example of what you can do with the Visio COM objects and powershell combined.
I have not yet included all the options I would like, in future versions I would like to be able to document the networking setup / storage paths etc but I’m working on something else so wanted to get this script out into the public.
Instructions
You will need:
- Powershell V1 (I haven’t tried it on V2 yet)
- The VI Toolkit
- Microsoft Visio (I have only tried 2003 +)
How to use:
- Download the zip file from the bottom of this page
- Once extracted copy the ‘My-VI-Shapes.vss‘ file to your ‘My Documents\My Shapes’ folder. If the folder does not exist create it and copy the file in.
- Run the powershell script with the following options:
To diagram the entire Infrastructure:
vDiagram.ps1 -VIServer MYVISERVER
To diagram a specific cluster use the following:
vDiagram.ps1 -VIServer MYVISERVER -Cluster “Production Cluster’
Example Output

The Download
about 1 year ago
Sweet ! This really saves some work. Thanks !
about 1 year ago
Hi Al,
Glad to see you’ve got it working!
- Chris (aka Visio Guy)
about 1 year ago
Thanks Chris, couldn’t have done it without your brilliant forum and help !
Hopefully others on your forum will see the power of using Visio and PowerShell together
Alan
about 1 year ago
Awesome stuff Alan,
Changed Line 93 from:
ForEach ($VM in (Get-vmhost $VMHost | get-vm))
To:
ForEach ($VM in (Get-vmhost $VMHost | get-vm -NoRecursion))
This fixes all VMs showing up under all hosts. Also fixed some of the Visio shapes with font size, etc. Was easy since I made the originals! Awesome awesome stuff.
about 1 year ago
Brilliant!
My current client has a bizarre policy of using very long VM names however for clones that they have produced for backup purposes. To get around that I have added the following:
1. A line beneath the $Savefile declaration:
[int]$maxtextlength = 15
2. A function in with the other functions:
function format-itemtext($item)
{
$itemtext = $item.Name
if($maxtextlength -lt [int]$itemtext.length)
{
$itemtext = $itemtext.substring(0,$maxtextlength) + “…”
}
return $itemtext
}
3. Modified one line in the function add-visioobject:
$shpObj.Text = format-itemtext($item)
The result is VM names truncated to 15 characters plus “…” to indicate that they are not the full name.
about 1 year ago
Nice One.
One quick question, what version of Visio are you using ?
Rob P
about 1 year ago
Looks familiar http://www.commandbreak.com/2007/10/14/VisioPowershellAutomagic.aspx
about 1 year ago
If 46 VMs on one host makes your diagram a little on the wide side, it is possible to wrap them onto additional rows.
The changes will take too long to describe here but it is possible.
about 1 year ago
Michael,
Sounds good, send the amendments over and I will put them in V2.
Alan
about 1 year ago
Joe,
Im so sorry, I knew I missed someone out of the credits, I have ammended the post to include a link to the great CommandBreak_ site. Thanks for all of Ben’s help.
Alan
about 1 year ago
Can someone post instructions on how to wrap the VMs to a second line (ie. 50 VMs on one host makes a big long diagram)…
about 1 year ago
Alan,
Nice work, especially given the always tough to find documentation for Visio. Can’t wait for version 2, maybe add a title to page?
gb
about 1 year ago
Errors out with:
Adding
Get-VMHost : The argument cannot be null or empty.
At C:\scripts\vDiagram.ps1:136 char:30
Any ideas?
about 1 year ago
For all german Users out there:
Chage Line 61 to “Meine Shapes” instead of “My Shapes”
about 1 year ago
Patrick, Thanks for your Translation
David, Can you send me a little detail about your envionment. Thanks
about 1 year ago
Alan,
For the “My Shapes” path, try this instead:
$stnPath = $AppVisio.MyShapesPath
Thanks for the great script.
–
Cameron
about 1 year ago
Cameron, Thanks, neat trick, BTW I think you may need to invest in some knee pads by the looks of your blog !
about 1 year ago
Hello is there anyway you can get this script to populate visio with the vmsdetails such as memory cpu and os?
about 1 year ago
Yeah this is easy enough and I was planning on a V2 of this script but….. If you check out what has been done in the PowerGUI Powerpack with this script and also look out on http://www.thevesi.org they will be adding this to there version (which is better than this one) in the near future.
Hope that helps.
about 1 year ago
Hello thanks for that however the power gui version seems to only work with visio 2007 and our desktop soe is office 2003.
about 1 year ago
Josh,
That is true for the moment buy I happen to have a little inside knowledge and don’t worry, you will be a happy man soon
about 1 year ago
How would this work in a large environment, say 110 ESX hosts and 1500 VMs? Would the resulting file be usable or printable? Maybe on a plotter?
about 1 year ago
@Bill
Bill, you may want to check out Thevesi.org as they have taken this script and enhanced it beyond belief, check here for a video of it in use: http://www.virtu-al.net/uo-vmug/
about 1 year ago
I am not able to download the zip file. Can some one plesae pass me?
Thanks,
Vikash
about 1 year ago
@Vikash Kumar
Emailed. Hope it works ok
about 1 year ago
As a Newbie, I am always searching online for articles that can help me. Thank you!
about 11 months ago
Al, dont see the zip file, how can i get it?
about 11 months ago
@Pushpal
I have changed the download link as was having problems with some people unable to access it. should be ok now.
about 11 months ago
Thanks Al!!
about 4 months ago
Sorry for the stupit question but I’ll get always this error:
The term ‘.\vDiagram.ps1′ is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:15
+ .\vDiagram.ps1 <<<< -VIServer em1apvm001
+ CategoryInfo : ObjectNotFound: (.\vDiagram.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Thanks
Guido
about 4 months ago
brilliant stuff!
is there a possibility to save the output also as html?
about 2 weeks ago
Mine asks for the ESX/Vcenter host, when I type it, Visio 2010 opens, but the diagram is blank?