<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>CommandBreak_ - Visio</title>
    <link>http://www.commandbreak.com/</link>
    <description>sometimes Next isn't enough</description>
    <language>en-us</language>
    <copyright>Ben Parker</copyright>
    <lastBuildDate>Sun, 14 Oct 2007 11:59:08 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>bparker@commandbreak.com</managingEditor>
    <webMaster>bparker@commandbreak.com</webMaster>
    <item>
      <trackback:ping>http://www.commandbreak.com/Trackback.aspx?guid=223ae58a-f4c2-429c-a2d0-4bb74bce2d58</trackback:ping>
      <pingback:server>http://www.commandbreak.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.commandbreak.com/PermaLink,guid,223ae58a-f4c2-429c-a2d0-4bb74bce2d58.aspx</pingback:target>
      <dc:creator>Ben Parker</dc:creator>
      <wfw:comment>http://www.commandbreak.com/CommentView,guid,223ae58a-f4c2-429c-a2d0-4bb74bce2d58.aspx</wfw:comment>
      <wfw:commentRss>http://www.commandbreak.com/SyndicationService.asmx/GetEntryCommentsRss?guid=223ae58a-f4c2-429c-a2d0-4bb74bce2d58</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I hate drawing Visio diagrams. I’m no Picasso and, for me, drawing a network diagram
is one of these necessary evils that I avoid doing as long as humanly possible.
</p>
        <p>
Not anymore, the good people at Microsoft have released the <a href="http://www.microsoft.com/downloads/details.aspx?familyid=cb42fc06-50c7-47ed-a65c-862661742764&amp;displaylang=en&amp;tm">Microsoft
Active Directory Topology Diagrammer</a> which dynamically creates Visio diagrams
for your Windows infrastructure. The topology tool reads your AD configuration, including
domains, sites, servers, administrative groups, routing groups and connectors (for
those with Exchange) and outputs a detailed library of diagram for you to manipulate
as required.
</p>
        <p>
Impressed with this release I spent some time google’ing other Visio automation tools,
chasing that illusive magic bullet to cure all my Visio pains, I found an abundance
of information and samples on <a href="http://msdn.microsoft.com/en-us/library/ms409183.aspx">MSDN</a>.
One such sample, <a href="http://support.microsoft.com/kb/309603">How to use OLE automation
in Visio</a>, is a VBA macro that did most of the grunt work, loading and created
a new Visio diagram, drawing a shape with a label, saving the changes and closing
Visio. Apart from being a macro it performed the key functionality that would be required
for any tool. To obtain the flexibility I was looking for I have transposed the macro
code to a Powershell script which we can expand on later.
</p>
        <blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
          <p>
            <font face="Arial">
              <font color="#006400"># ' Create an instance of Visio and create
a document based on the<br />
# ' Basic Diagram template. It doesn't matter if an instance of<br />
# ' Visio is already running, CreateObject will run a new one.<br />
# Set AppVisio = CreateObject("visio.application")<br /></font>
              <font color="#800080">$AppVisio</font> = <font color="#0000ff">New-Object -ComObject</font> Visio.Application</font>
          </p>
          <p>
            <font face="Arial">
              <font color="#006400"># Set docsObj = AppVisio.Documents</font>
              <br />
              <font color="#800080">$docsObj</font> =<font color="#800080"> $AppVisio</font>.Documents</font>
          </p>
          <p>
            <font face="Arial">
              <font color="#006400"># ' Create a document based on the Basic
Diagram template that<br />
# ' automatically opens the Basic Shapes stencil.<br />
# Set DocObj = docsObj.Add("Basic Diagram.vst")<br /></font>
              <font color="#800080">$DocObj</font> =<font color="#800080"> $docsObj</font>.Add(<font color="#ff0000">"Basic
Diagram.vst"</font>)</font>
          </p>
          <p>
            <font face="Arial">
              <font color="#006400"># Set pagsObj = AppVisio.ActiveDocument.Pages</font>
              <br />
              <font color="#800080">$pagsObj</font> = <font color="#800080">$AppVisio</font>.ActiveDocument.Pages</font>
          </p>
          <p>
            <font face="Arial">
              <font color="#006400"># ' A new document always has at least one
page, whose index in the<br />
# ' Pages collection is 1.<br />
# Set pagObj = pagsObj.Item(1)<br /></font>
              <font color="#800080">$pagObj</font> = <font color="#800080">$pagsObj</font>.Item(1)</font>
          </p>
          <p>
            <font face="Arial">
              <font color="#006400"># Set stnObj = AppVisio.Documents("Basic
Shapes.vss")<br /></font>
              <font color="#800080">$stnObj</font> = <font color="#800080">$AppVisio</font>.Documents.Add(<font color="#ff0000">"Basic
Shapes.vss"</font>)</font>
          </p>
          <p>
            <font face="Arial">
              <font color="#006400"># Set mastObj = stnObj.Masters("Rectangle")</font>
              <br />
              <font color="#800080">$mastObj</font> = <font color="#800080">$stnObj</font>.Masters.Item(<font color="#ff0000">"Rectangle"</font>)</font>
          </p>
          <p>
            <font face="Arial">
              <font color="#006400"># ' Drop the rectangle in the approximate
middle of the page.<br />
# ' Coordinates passed with the Drop method are always inches.<br />
# Set shpObj = pagObj.Drop(mastObj, 4.25, 5.5)<br /></font>
              <font color="#800080">$shpObj</font> = <font color="#800080">$pagObj</font>.Drop(<font color="#800080">$mastObj</font>,
4.25, 5.5)</font>
          </p>
          <p>
            <font face="Arial">
              <font color="#006400"># ' Set the text of the rectangle.<br />
# shpObj.Text = "This is some text."<br /></font>
              <font color="#800080">$shpObj</font>.Text = <font color="#ff0000">"This is
some text."</font></font>
          </p>
          <p>
            <font face="Arial">
              <font color="#006400"># ' Save the drawing and quit Visio. The
message pauses the program<br />
# ' so you can see the Visio drawing before the instance closes.<br />
# DocObj.SaveAs "MyDrawing.vsd"<br /></font>
              <font color="#800080">$DocObj</font>.SaveAs(<font color="#ff0000">"C:\MyDrawing.vsd"</font>)<br /><font color="#006400"># MsgBox "Drawing finished!", , "AutoVisio (OLE) Example"</font></font>
          </p>
          <p>
            <font face="Arial">
              <font color="#006400"># ' Quit Visio.<br />
# AppVisio.Quit</font>
              <br />
              <font color="#800080">$AppVisio</font>.Quit()</font>
          </p>
          <p>
            <font face="Arial" color="#006400"># ' Clear the variable from memory.<br />
# Set AppVisio = Nothing</font>
          </p>
        </blockquote>
        <p>
In an effort to make it that little bit easier to understand I have commented all
the sample code and incorporated the Powershell equivalents. Remember that
Powershell, unlike VB 6, doesn’t support default properties. So when we are calling
a method (e.g. <font face="Arial"><font color="#800080">$stnObj</font>.Masters</font>)
we have to define the property we are setting (e.g. <font face="Arial">Item</font>).
</p>
        <p>
So great I have a rectangle with some text in the middle of a blank Visio, not very
useful, but after a few changes we have something that will dynamically generate stencils
for all computers in a domain.
</p>
        <blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
          <p>
            <font face="Arial">
              <font color="#006400"># Zero initial drop coordinates</font>
              <br />
              <font color="#800080">$x</font> = 0<br /><font color="#800080">$y</font> = 1.20</font>
          </p>
          <p>
            <font face="Arial">
              <font color="#006400"># Create an instance of Visio and create
a document based on the Basic Diagram template.<br /></font>
              <font color="#800080">$AppVisio</font> = <font color="#0000ff">New-Object -ComObject</font> Visio.Application<br /><font color="#800080">$docsObj</font> = <font color="#800080">$AppVisio</font>.Documents<br /><font color="#800080">$DocObj</font> = <font color="#800080">$docsObj</font>.Add(<font color="#ff0000">"Basic
Diagram.vst"</font>)</font>
          </p>
          <p>
            <font face="Arial">
              <font color="#006400"># Set the active page of the document to
page 1<br /></font>
              <font color="#800080">$pagsObj</font> = <font color="#800080">$AppVisio</font>.ActiveDocument.Pages<br /><font color="#800080">$pagObj</font> = <font color="#800080">$pagsObj</font>.Item(1)</font>
          </p>
          <p>
            <font face="Arial">
              <font color="#006400"># Load a set of stencils and select one to
drop</font>
              <br />
              <font color="#800080">$stnObj</font> = <font color="#800080">$AppVisio</font>.Documents.Add(<font color="#ff0000">"SERVER_M.vss"</font>)<br /><font color="#800080">$mastObj</font> = <font color="#800080">$stnObj</font>.Masters.Item(<font color="#ff0000">"Server"</font>)</font>
          </p>
          <p>
            <font face="Arial" color="#006400"># Retrieve a list of computer accounts from Active
Directory 
<br />
# </font>
            <a href="http://www.microsoft.com/technet/scriptcenter/resources/qanda/nov06/hey1109.mspx">
              <font face="Arial" color="#006400">http://www.microsoft.com/technet/scriptcenter/resources/qanda/nov06/hey1109.mspx</font>
            </a>
          </p>
          <p>
            <font face="Arial">
              <font color="#800080">$strCategory</font> = "computer"</font>
          </p>
          <p>
            <font face="Arial">
              <font color="#800080">$objDomain</font> = <font color="#0000ff">New-Object</font> System.DirectoryServices.DirectoryEntry</font>
          </p>
          <p>
            <font face="Arial">
              <font color="#800080">$objSearcher</font> = <font color="#0000ff">New-Object</font> System.DirectoryServices.DirectorySearcher<br /><font color="#800080">$objSearcher</font>.SearchRoot = <font color="#800080">$objDomain</font><br /><font color="#800080">$objSearcher</font>.Filter = (<font color="#ff0000">"(objectCategory=</font><font color="#800080">$strCategory</font><font color="#ff0000">)"</font>)</font>
          </p>
          <p>
            <font face="Arial">
              <font color="#800080">$colProplist</font> = <font color="#ff0000">"name"<br /></font><font color="#0000ff">foreach</font> (<font color="#800080">$i</font><font color="#0000ff">in</font><font color="#800080">$colPropList</font>){<font color="#800080">$objSearcher</font>.PropertiesToLoad.Add(<font color="#800080">$i</font>)}</font>
          </p>
          <p>
            <font face="Arial">
              <font color="#800080">$colResults</font> = <font color="#800080">$objSearcher</font>.FindAll()</font>
          </p>
          <p>
            <font face="Arial">
              <font color="#0000ff">foreach</font> (<font color="#800080">$objResult</font><font color="#0000ff">in</font><font color="#800080">$colResults</font>)<br />
{<br />
  <font color="#800080">$objComputer</font> = <font color="#800080">$objResult</font>.Properties; <font color="#800080">$objComputer</font>.name<br />
  <br />
  <font color="#006400"># Set coordinates for drop</font><br />
  <font color="#800080">$x</font> = <font color="#800080">$x</font> + 1.30<br />
  <br />
 <font color="#006400"> # Loop to the next line</font><br />
  <font color="#0000ff">if</font>(<font color="#800080">$x</font><font color="#ffa500">-ge</font> 8)<br />
  {<br />
   <font color="#800080">$x</font> = 1.30<br />
   <font color="#800080">$y</font> = <font color="#800080">$y</font> +
1.20<br />
  }<br />
  <br />
 <font color="#006400"> # Drop the selected stencil on the active page,
with the coordinates x, y<br /></font>  <font color="#800080">$shpObj</font> = <font color="#800080">$pagObj</font>.Drop(<font color="#800080">$mastObj</font>, <font color="#800080">$x</font>, <font color="#800080">$y</font>)<br />
  <br />
 <font color="#006400"> # Enter text for the object<br /></font> <font color="#800080"> $shpObj</font>.Text = <font color="#800080">$objComputer.name</font><br />
 }</font>
          </p>
          <p>
            <font face="Arial">
              <font color="#006400"># Save the diagram<br /></font>
              <font color="#800080">$DocObj</font>.SaveAs(<font color="#ff0000">"C:\CMDBREAK_MyDrawing.vsd"</font>)</font>
          </p>
          <p>
            <font face="Arial">
              <font color="#006400"># Quit Visio<br /></font>
              <font color="#800080">$AppVisio</font>.Quit()</font>
          </p>
        </blockquote>
        <p>
Lost? 
</p>
        <p align="center">
          <img src="http://www.commandbreak.com/content/binary/Visio_Powershell=automagic.png" border="0" />
        </p>
        <p>
Highlighted above in each of the colours is the following:<br />
Yellow – What we started with, only modified to select and drop the Server stencil.<br />
Red – Controls where we drop each stencil, so as they are not on top of each other.<br />
Blue - Enumerates all computer accounts in the local domain. Detailed explanation
is available at <a href="http://www.microsoft.com/technet/scriptcenter/resources/qanda/nov06/hey1109.mspx">Hey,
Scripting Guy! How Can I Use Windows PowerShell to Get a List of All My Computers?</a></p>
        <p>
Now we have a script that will retrieve all the computer accounts in an AD domain
and draw shapes for each, one of the monotonous tasks required for any network diagram.
</p>
        <p>
To recap we have converted a VBA macro to a Powershell script and after a few additions
we can generate a set of Server stencils labelled with the hostnames of all computers
connected to an AD domain. This is just a start, with some more modifications it would
be easy to select a different stencil based on the services running or have the script
modify existing shapes in a drawing to create a dynamic network diagram.
</p>
        <p>
Happy Visio Automagic.
</p>
        <img width="0" height="0" src="http://www.commandbreak.com/aggbug.ashx?id=223ae58a-f4c2-429c-a2d0-4bb74bce2d58" />
      </body>
      <title>Visio + Powershell = Automagic</title>
      <guid isPermaLink="false">http://www.commandbreak.com/PermaLink,guid,223ae58a-f4c2-429c-a2d0-4bb74bce2d58.aspx</guid>
      <link>http://www.commandbreak.com/2007/10/14/VisioPowershellAutomagic.aspx</link>
      <pubDate>Sun, 14 Oct 2007 11:59:08 GMT</pubDate>
      <description>&lt;p&gt;
I hate drawing Visio diagrams. I’m no Picasso and, for me, drawing a network diagram
is one of these necessary evils that I avoid doing as long as humanly possible.
&lt;/p&gt;
&lt;p&gt;
Not anymore, the good people at Microsoft have released the &lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=cb42fc06-50c7-47ed-a65c-862661742764&amp;amp;displaylang=en&amp;amp;tm"&gt;Microsoft
Active Directory Topology Diagrammer&lt;/a&gt; which dynamically creates Visio diagrams
for your Windows infrastructure. The topology tool reads your AD configuration, including
domains, sites, servers, administrative groups, routing groups and connectors (for
those with Exchange) and outputs a detailed library of diagram for you to manipulate
as required.
&lt;/p&gt;
&lt;p&gt;
Impressed with this release I spent some time google’ing other Visio automation tools,
chasing that illusive magic bullet&amp;nbsp;to cure all my Visio pains, I found an abundance
of information and samples on &lt;a href="http://msdn.microsoft.com/en-us/library/ms409183.aspx"&gt;MSDN&lt;/a&gt;.
One such sample, &lt;a href="http://support.microsoft.com/kb/309603"&gt;How to use OLE automation
in Visio&lt;/a&gt;, is a VBA macro that did most of the grunt work, loading and created
a new Visio diagram, drawing a shape with a label, saving the changes and closing
Visio. Apart from being a macro it performed the key functionality that would be required
for any tool. To obtain the flexibility I was looking for I have transposed the macro
code to a Powershell script which we can expand on later.
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#006400&gt;# ' Create an instance of Visio and create a
document based on the&lt;br&gt;
# ' Basic Diagram template. It doesn't matter if an instance of&lt;br&gt;
# ' Visio is already running, CreateObject will run a new one.&lt;br&gt;
# Set AppVisio = CreateObject("visio.application")&lt;br&gt;
&lt;/font&gt;&lt;font color=#800080&gt;$AppVisio&lt;/font&gt; = &lt;font color=#0000ff&gt;New-Object -ComObject&lt;/font&gt; Visio.Application&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#006400&gt;# Set docsObj = AppVisio.Documents&lt;/font&gt;
&lt;br&gt;
&lt;font color=#800080&gt;$docsObj&lt;/font&gt; =&lt;font color=#800080&gt; $AppVisio&lt;/font&gt;.Documents&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#006400&gt;# ' Create a document based on the Basic Diagram
template that&lt;br&gt;
# ' automatically opens the Basic Shapes stencil.&lt;br&gt;
# Set DocObj = docsObj.Add("Basic Diagram.vst")&lt;br&gt;
&lt;/font&gt;&lt;font color=#800080&gt;$DocObj&lt;/font&gt; =&lt;font color=#800080&gt; $docsObj&lt;/font&gt;.Add(&lt;font color=#ff0000&gt;"Basic
Diagram.vst"&lt;/font&gt;)&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#006400&gt;# Set pagsObj = AppVisio.ActiveDocument.Pages&lt;/font&gt;
&lt;br&gt;
&lt;font color=#800080&gt;$pagsObj&lt;/font&gt; = &lt;font color=#800080&gt;$AppVisio&lt;/font&gt;.ActiveDocument.Pages&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#006400&gt;# ' A new document always has at least one page,
whose index in the&lt;br&gt;
# ' Pages collection is 1.&lt;br&gt;
# Set pagObj = pagsObj.Item(1)&lt;br&gt;
&lt;/font&gt;&lt;font color=#800080&gt;$pagObj&lt;/font&gt; = &lt;font color=#800080&gt;$pagsObj&lt;/font&gt;.Item(1)&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#006400&gt;# Set stnObj = AppVisio.Documents("Basic Shapes.vss")&lt;br&gt;
&lt;/font&gt;&lt;font color=#800080&gt;$stnObj&lt;/font&gt; = &lt;font color=#800080&gt;$AppVisio&lt;/font&gt;.Documents.Add(&lt;font color=#ff0000&gt;"Basic
Shapes.vss"&lt;/font&gt;)&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#006400&gt;# Set mastObj = stnObj.Masters("Rectangle")&lt;/font&gt;
&lt;br&gt;
&lt;font color=#800080&gt;$mastObj&lt;/font&gt; = &lt;font color=#800080&gt;$stnObj&lt;/font&gt;.Masters.Item(&lt;font color=#ff0000&gt;"Rectangle"&lt;/font&gt;)&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#006400&gt;# ' Drop the rectangle in the approximate middle
of the page.&lt;br&gt;
# ' Coordinates passed with the Drop method are always inches.&lt;br&gt;
# Set shpObj = pagObj.Drop(mastObj, 4.25, 5.5)&lt;br&gt;
&lt;/font&gt;&lt;font color=#800080&gt;$shpObj&lt;/font&gt; = &lt;font color=#800080&gt;$pagObj&lt;/font&gt;.Drop(&lt;font color=#800080&gt;$mastObj&lt;/font&gt;,
4.25, 5.5)&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#006400&gt;# ' Set the text of the rectangle.&lt;br&gt;
# shpObj.Text = "This is some text."&lt;br&gt;
&lt;/font&gt;&lt;font color=#800080&gt;$shpObj&lt;/font&gt;.Text = &lt;font color=#ff0000&gt;"This is some
text."&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#006400&gt;# ' Save the drawing and quit Visio. The message
pauses the program&lt;br&gt;
# ' so you can see the Visio drawing before the instance closes.&lt;br&gt;
# DocObj.SaveAs "MyDrawing.vsd"&lt;br&gt;
&lt;/font&gt;&lt;font color=#800080&gt;$DocObj&lt;/font&gt;.SaveAs(&lt;font color=#ff0000&gt;"C:\MyDrawing.vsd"&lt;/font&gt;)&lt;br&gt;
&lt;font color=#006400&gt;# MsgBox "Drawing finished!", , "AutoVisio (OLE) Example"&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#006400&gt;# ' Quit Visio.&lt;br&gt;
# AppVisio.Quit&lt;/font&gt;
&lt;br&gt;
&lt;font color=#800080&gt;$AppVisio&lt;/font&gt;.Quit()&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial color=#006400&gt;# ' Clear the variable from memory.&lt;br&gt;
# Set AppVisio = Nothing&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
In an effort to make it that little bit easier to understand I have commented all
the sample code and incorporated the Powershell equivalents.&amp;nbsp;Remember&amp;nbsp;that
Powershell, unlike VB 6, doesn’t support default properties. So when we are calling
a method (e.g. &lt;font face=Arial&gt;&lt;font color=#800080&gt;$stnObj&lt;/font&gt;.Masters&lt;/font&gt;)
we have to define the property we are setting (e.g. &lt;font face=Arial&gt;Item&lt;/font&gt;).
&lt;/p&gt;
&lt;p&gt;
So great I have a rectangle with some text in the middle of a blank Visio, not very
useful, but after a few changes we have something that will dynamically generate stencils
for all computers in a domain.
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#006400&gt;# Zero initial drop coordinates&lt;/font&gt;
&lt;br&gt;
&lt;font color=#800080&gt;$x&lt;/font&gt; = 0&lt;br&gt;
&lt;font color=#800080&gt;$y&lt;/font&gt; = 1.20&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#006400&gt;# Create an instance of Visio and create a document
based on the Basic Diagram template.&lt;br&gt;
&lt;/font&gt;&lt;font color=#800080&gt;$AppVisio&lt;/font&gt; = &lt;font color=#0000ff&gt;New-Object -ComObject&lt;/font&gt; Visio.Application&lt;br&gt;
&lt;font color=#800080&gt;$docsObj&lt;/font&gt; = &lt;font color=#800080&gt;$AppVisio&lt;/font&gt;.Documents&lt;br&gt;
&lt;font color=#800080&gt;$DocObj&lt;/font&gt; = &lt;font color=#800080&gt;$docsObj&lt;/font&gt;.Add(&lt;font color=#ff0000&gt;"Basic
Diagram.vst"&lt;/font&gt;)&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#006400&gt;# Set the active page of the document to page
1&lt;br&gt;
&lt;/font&gt;&lt;font color=#800080&gt;$pagsObj&lt;/font&gt; = &lt;font color=#800080&gt;$AppVisio&lt;/font&gt;.ActiveDocument.Pages&lt;br&gt;
&lt;font color=#800080&gt;$pagObj&lt;/font&gt; = &lt;font color=#800080&gt;$pagsObj&lt;/font&gt;.Item(1)&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#006400&gt;# Load a set of stencils and select one to drop&lt;/font&gt;
&lt;br&gt;
&lt;font color=#800080&gt;$stnObj&lt;/font&gt; = &lt;font color=#800080&gt;$AppVisio&lt;/font&gt;.Documents.Add(&lt;font color=#ff0000&gt;"SERVER_M.vss"&lt;/font&gt;)&lt;br&gt;
&lt;font color=#800080&gt;$mastObj&lt;/font&gt; = &lt;font color=#800080&gt;$stnObj&lt;/font&gt;.Masters.Item(&lt;font color=#ff0000&gt;"Server"&lt;/font&gt;)&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial color=#006400&gt;# Retrieve a list of computer accounts from Active
Directory 
&lt;br&gt;
# &lt;/font&gt;&lt;a href="http://www.microsoft.com/technet/scriptcenter/resources/qanda/nov06/hey1109.mspx"&gt;&lt;font face=Arial color=#006400&gt;http://www.microsoft.com/technet/scriptcenter/resources/qanda/nov06/hey1109.mspx&lt;/font&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#800080&gt;$strCategory&lt;/font&gt; = "computer"&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#800080&gt;$objDomain&lt;/font&gt; = &lt;font color=#0000ff&gt;New-Object&lt;/font&gt; System.DirectoryServices.DirectoryEntry&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#800080&gt;$objSearcher&lt;/font&gt; = &lt;font color=#0000ff&gt;New-Object&lt;/font&gt; System.DirectoryServices.DirectorySearcher&lt;br&gt;
&lt;font color=#800080&gt;$objSearcher&lt;/font&gt;.SearchRoot = &lt;font color=#800080&gt;$objDomain&lt;/font&gt;
&lt;br&gt;
&lt;font color=#800080&gt;$objSearcher&lt;/font&gt;.Filter = (&lt;font color=#ff0000&gt;"(objectCategory=&lt;/font&gt;&lt;font color=#800080&gt;$strCategory&lt;/font&gt;&lt;font color=#ff0000&gt;)"&lt;/font&gt;)&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#800080&gt;$colProplist&lt;/font&gt; = &lt;font color=#ff0000&gt;"name"&lt;br&gt;
&lt;/font&gt;&lt;font color=#0000ff&gt;foreach&lt;/font&gt; (&lt;font color=#800080&gt;$i&lt;/font&gt; &lt;font color=#0000ff&gt;in&lt;/font&gt; &lt;font color=#800080&gt;$colPropList&lt;/font&gt;){&lt;font color=#800080&gt;$objSearcher&lt;/font&gt;.PropertiesToLoad.Add(&lt;font color=#800080&gt;$i&lt;/font&gt;)}&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#800080&gt;$colResults&lt;/font&gt; = &lt;font color=#800080&gt;$objSearcher&lt;/font&gt;.FindAll()&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#0000ff&gt;foreach&lt;/font&gt; (&lt;font color=#800080&gt;$objResult&lt;/font&gt; &lt;font color=#0000ff&gt;in&lt;/font&gt; &lt;font color=#800080&gt;$colResults&lt;/font&gt;)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;font color=#800080&gt;$objComputer&lt;/font&gt; = &lt;font color=#800080&gt;$objResult&lt;/font&gt;.Properties; &lt;font color=#800080&gt;$objComputer&lt;/font&gt;.name&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;font color=#006400&gt;# Set coordinates for drop&lt;/font&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;font color=#800080&gt;$x&lt;/font&gt; = &lt;font color=#800080&gt;$x&lt;/font&gt; + 1.30&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&lt;font color=#006400&gt;&amp;nbsp;# Loop to the next line&lt;/font&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;font color=#0000ff&gt;if&lt;/font&gt;(&lt;font color=#800080&gt;$x&lt;/font&gt; &lt;font color=#ffa500&gt;-ge&lt;/font&gt; 8)&lt;br&gt;
&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=#800080&gt;$x&lt;/font&gt; = 1.30&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color=#800080&gt;$y&lt;/font&gt; = &lt;font color=#800080&gt;$y&lt;/font&gt; +
1.20&lt;br&gt;
&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&lt;font color=#006400&gt;&amp;nbsp;# Drop the selected stencil on the active page, with
the coordinates x, y&lt;br&gt;
&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color=#800080&gt;$shpObj&lt;/font&gt; = &lt;font color=#800080&gt;$pagObj&lt;/font&gt;.Drop(&lt;font color=#800080&gt;$mastObj&lt;/font&gt;, &lt;font color=#800080&gt;$x&lt;/font&gt;, &lt;font color=#800080&gt;$y&lt;/font&gt;)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&lt;font color=#006400&gt;&amp;nbsp;# Enter text for the object&lt;br&gt;
&lt;/font&gt;&amp;nbsp;&lt;font color=#800080&gt;&amp;nbsp;$shpObj&lt;/font&gt;.Text = &lt;font color=#800080&gt;$objComputer.name&lt;/font&gt;
&lt;br&gt;
&amp;nbsp;}&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#006400&gt;# Save the diagram&lt;br&gt;
&lt;/font&gt;&lt;font color=#800080&gt;$DocObj&lt;/font&gt;.SaveAs(&lt;font color=#ff0000&gt;"C:\CMDBREAK_MyDrawing.vsd"&lt;/font&gt;)&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Arial&gt;&lt;font color=#006400&gt;# Quit Visio&lt;br&gt;
&lt;/font&gt;&lt;font color=#800080&gt;$AppVisio&lt;/font&gt;.Quit()&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Lost? 
&lt;/p&gt;
&lt;p align=center&gt;
&lt;img src="http://www.commandbreak.com/content/binary/Visio_Powershell=automagic.png" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
Highlighted above in each of the colours is the following:&lt;br&gt;
Yellow – What we started with, only modified to select and drop the Server stencil.&lt;br&gt;
Red – Controls where we drop each stencil, so as they are not on top of each other.&lt;br&gt;
Blue - Enumerates all computer accounts in the local domain. Detailed explanation
is available at &lt;a href="http://www.microsoft.com/technet/scriptcenter/resources/qanda/nov06/hey1109.mspx"&gt;Hey,
Scripting Guy! How Can I Use Windows PowerShell to Get a List of All My Computers?&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
Now we have a script that will retrieve all the computer accounts in an AD domain
and draw shapes for each, one of the monotonous tasks required for any network diagram.
&lt;/p&gt;
&lt;p&gt;
To recap we have converted a VBA macro to a Powershell script and after a few additions
we can generate a set of Server stencils labelled with the hostnames of all computers
connected to an AD domain. This is just a start, with some more modifications it would
be easy to select a different stencil based on the services running or have the script
modify existing shapes in a drawing to create a dynamic network diagram.
&lt;/p&gt;
&lt;p&gt;
Happy Visio Automagic.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.commandbreak.com/aggbug.ashx?id=223ae58a-f4c2-429c-a2d0-4bb74bce2d58" /&gt;</description>
      <comments>http://www.commandbreak.com/CommentView,guid,223ae58a-f4c2-429c-a2d0-4bb74bce2d58.aspx</comments>
      <category>Powershell</category>
      <category>Visio</category>
    </item>
  </channel>
</rss>