PoshCode Logo PowerShell Code Repository

Get-ADMapObject (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/706"></script>download | new post

Map Active Directory objects using the Show-NetMap script from Doug Finke. Running the script creates three functions Get-ADMapObject (which takes a string or array of strings of the AD object classes), Get-ADObjectClassName (recurses through your Active Directory and returns the names of the object classes), and New-SourceTarget (to get the ADMapObjects into the format that Show-Netmap wants).
Before the script runs, it will check for the dependencies (Doug’s script and the NetMap dll’s) and advise you if there are any missing.

  1. # Author: Steven Murawski http://www.mindofroot.com
  2. # This script requires the Show-NetMap script from Doug Finke and the NetMap files
  3. # These can be found at http://dougfinke.com/blog/?p=465
  4. #
  5. # Also required are the Quest AD Cmdlets.
  6.  
  7. #requires -pssnapin Quest.ActiveRoles.ADManagement
  8.  
  9.  
  10.  
  11. function Write-Help()
  12. {
  13.         $ExampleUsage = @'
  14. To use this script, you will need the Show-Netmap script from Doug Finke,
  15. along with the NetMap DLLs (included with the Show-NetMap script on Doug's blog).
  16. Downloadable from http://dougfinke.com/blog/?p=465
  17.  
  18. Usage:
  19. . .\Get-ADMapObject.ps1
  20. Get-ADMapObject ([Object Class Name] | [Array of Object Class Names]) | % { New-SourceTarget $_.Name $_.Parent } | Show-NetMap
  21.  
  22.  
  23. Example:
  24. . .\Get-ADMapObject.ps1
  25. Get-ADMapObject group | % { New-SourceTarget $_.Name $_.Parent } | Show-NetMap -layoutType G
  26. Get-ADMapObject ou, group, user | % { New-SourceTarget $_.Name $_.Parent } | Show-NetMap -layoutType S
  27.  
  28. If you would like to get a listing (or array) of the Object Class Names, use the Get-ADObjectClassName function
  29.  
  30. $classes = Get-ADObjectClassName
  31.  
  32. '@
  33.         Write-Host $ExampleUsage
  34. }
  35.  
  36. #Check to see if the required files are present to run the script.
  37. function Test-Prerequisites()
  38. {
  39.         $required = @{ShowNetMap = 'Show-Netmap.ps1';
  40.                 NetMapApp = 'Microsoft.NetMap.ApplicationUtil.dll';
  41.                 NetMapControl = 'Microsoft.NetMap.Control.dll';
  42.                 NetMapCore =  'Microsoft.NetMap.Core.dll';
  43.                 NetMapUtil =  'Microsoft.NetMap.Util.dll';
  44.                 NetMapVisual =  'Microsoft.NetMap.Visualization.dll'
  45.                 }
  46.                
  47.         $report = @()
  48.        
  49.         foreach ($key in $required.Keys)
  50.         {
  51.                 $file = $required[$($key)]
  52.                 if (Test-Path $file )
  53.                 {
  54.                         Write-Debug "Found $file"
  55.                 }
  56.                 else
  57.                 {
  58.                         $report +=  "Missing $file"
  59.                 }
  60.                
  61.         }
  62.        
  63.         if ($report.count -eq 0)
  64.         {
  65.                 Write-Debug "All prerequisites were found."
  66.                 return $true
  67.         }
  68.         else
  69.         {
  70.                 Write-Help
  71.                 Write-Host "Missing files: "
  72.                 $report | ForEach-Object { Write-Host "`tMissing $_" }
  73.                 throw "Please move the needed files into the current directory!"
  74.         }
  75.  
  76.  
  77. }
  78.  
  79. #If all the prereq's are in the local directory and the script was not run
  80. #with the -help switch, load everything up!
  81. if (Test-Prerequisites)
  82. {
  83.         #Add the Show-Netmap functions from Doug Finke
  84.         . .\Show-NetMap.ps1
  85.        
  86.         #This is just a helper function to find the parent of an object based on the parent's distinguished name
  87.         function Get-ParentFromDN()
  88.         {
  89.                 PROCESS
  90.                 {
  91.                         $root = '^DC=(\w+),DC=(\w+)$'
  92.                         $pattern = '^(OU|CN)=(\w+?),.*?DC=\w+?,DC=\w+?$'
  93.                         $dn = $_
  94.                        
  95.                        
  96.                         if ($dn -notmatch $root)
  97.                         {
  98.                                 $dn -replace $pattern, '$2'
  99.                         }
  100.                         else
  101.                         {
  102.                                 $dn -replace $root, '$1.$2'
  103.                         }
  104.                 }
  105.         }
  106.        
  107.         #This will return an array of all the Object Classes in your Active Directory
  108.         function Get-ADObjectClassName()
  109.         {
  110.                 Get-QADObject | Select-Object -property @{name='Name';Expression={$_.type}} -unique | Sort-Object
  111.         }
  112.        
  113.         function Get-ADMapObject()
  114.         {
  115.                 param($TypesToMap=$(Throw 'One (or more object types as an array) are required to run this function'),
  116.                 [switch]$help)
  117.                 if ($help)
  118.                 {
  119.                         Write-Help
  120.                 }
  121.                
  122.                 if ($TypesToMap -is [string])
  123.                 {
  124.                         Get-QADObject -Type $TypesToMap | select Name, @{name='Parent';Expression={$_.ParentContainerDN | Get-ParentFromDN}}
  125.                 }
  126.                 else
  127.                 {
  128.                         foreach ( $type in $TypesToMap )
  129.                         {
  130.                                 Get-QADObject -Type $type | select Name, @{name='Parent';Expression={$_.ParentContainerDN | Get-ParentFromDN}}
  131.                         }
  132.                 }
  133.         }
  134.        
  135.         #Helper function stolen from Doug Finke and used to create the objects to feed to
  136.         #Show-Netmap
  137.         function New-SourceTarget ($s,$t)
  138.         {
  139.                 New-Object PSObject |
  140.                         Add-Member -pass noteproperty source $s |
  141.                         Add-Member -pass noteproperty target $t
  142.         }
  143.        
  144. }
  145.  
  146. Write-Help

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me