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