PowerShell and Visio – Documenting AD: 3
Up to now we have been dealing with a single level of OUs. There are few AD implementations that don’t have child OUs so how do we deal with them.
| 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 063 064 065 066 067 068 069 070 071 072 073 074
| 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, $parent ) $ou = $page.Drop($orgunit, $x, $y) $ou.Resize(1, 5, 70) $ou.Text = $name $link = $page.Drop($dircon,1,$y) $start = $link.CellsU("BeginX").GlueTo($parent.CellsU("PinX")) $end = $link.CellsU("EndX").GlueTo($ou.CellsU("PinX")) return $ou } $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 $x = 1 foreach ($ou in $ous) { $names = $ou -split "," $ouname = $names[0] -replace "ou=", "" $parent = $names[1].Remove(0,3) #$parent $y = $y - 0.75 if ($parent -eq $domname) { New-Variable -Name "$ouname" -Value (Add-ou $ouname ($x + 0.5) $y $dom) -Force } else { $linkto = Get-Variable -Name $parent -ValueOnly New-Variable -Name "$ouname" -Value (Add-ou $ouname ($x + $names.length -2.5 ) $y $linkto) -Force } } |
The difference here is in the way we handle the OUs. We get the parent of the OU we are working with. If the parent is the domain we link back to that as before. If its another OU we link to that.
A variable is created for every OU object in the diagram at the time we create it. We can then use get-variable to find the value of the variable to be our parent for linking