Categories

PowerShell and Visio – Documenting AD: 2

I’ve taken the script to add OUs to a drawing and modified it so that the addition is performed by a function.

I started with a text file containing some OUs

ou=England,dc=Maticore,dc=org
ou=Scotland,dc=Maticore,dc=org
ou=Wales,dc=Maticore,dc=org
ou=Ireland,dc=Maticore,dc=org
ou=Portugal,dc=Maticore,dc=org
ou=Belgium,dc=Maticore,dc=org
ou=Latvia,dc=Maticore,dc=org

Notice that they are all top level OUs – lets not get too ambitious!

 

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
function Add-Domain {
 param (
    [string]$name
 )
    $dom = $page.Drop($domain, 1, 11)
    $dom.Resize(1, 5, 70)
    $dom.Text = $name
    return $dom
 
}

function Add-Ou {
 param (
    [string]$name,
    [double]$x,
    [double]$y
)
    $ou = $page.Drop($orgunit, $x, $y)
    $ou.Resize(1, 5, 70)
    $ou.Text = $name   

    $dom_ou = $page.Drop($dircon,1,$y)
    $start = $dom_ou.CellsU("BeginX").GlueTo($dom.CellsU("PinX"))
    $end = $dom_ou.CellsU("EndX").GlueTo($ou.CellsU("PinX"))
       
}

$visio = New-Object -ComObject Visio.Application
$docs = $visio.Documents

## use blank drawing
$doc = $docs.Add("")

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

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

## Add objects
$domain = $stencil.Masters.Item("Domain")
$orgunit = $stencil.Masters.Item("Organizational Unit")
$dircon = $stencil.Masters.Item("Directory connector")

$file = "manticore.txt"
$domname = ($file -split "\.")[0]

$ous = Get-Content $file

$dom = Add-Domain $domname

$y = 11

foreach ($ou in $ous) {
    $ouname = ($ou -split ",")[0] -replace "ou=", ""
   
    $y = $y - 0.75
      
    Add-ou $ouname 1.5 $y
}

The functions add the domain and OU objects to the drawing.  Only change is that I set the text property of the OU and domain.

The main body of the script creates the drawing as before.  Uses the file name to get the domain and then reads the file. It splits out the OU names and calculates the Y co-ordinate then adds the OU

Next up I need to deal with child OUs

Leave a Reply