PoshCode Logo PowerShell Code Repository

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

This is a modification of the Get-ADMapObject (http://poshcode.org/706). This script uses the Show-NodeXLMap script (http://poshcode.org/733) to display the hierarchical relationship between Active Directory objects of the selected classes. Running this script with the -ShowADClass switch lists out the different classes or types of AD objects available.

  1. # Author: Steven Murawski http://www.mindofroot.com
  2. # This script requires the modified Show-NetMap script (originally from Doug Finke)
  3. # Show-NodeXLMap which can be found on http://poshcode.org/733
  4. # and the NodeXL files
  5. # These can be found at http://www.codeplex.com/NodeXL/Release/ProjectReleases.aspx?ReleaseId=20494
  6. #
  7. # Also required are the Quest AD Cmdlets.
  8.  
  9. #requires -pssnapin Quest.ActiveRoles.ADManagement
  10. param ($ADClass, $MapLayout= "circular", [switch]$help, [switch]$ShowADClass, [switch]$Colorize, [switch]$NoTest)
  11.  
  12. function Write-Help()
  13. {
  14.         $ExampleUsage = @'
  15. To use this script, you will need the Show-Netmap script from Doug Finke,
  16. along with the NodeXL DLLs.
  17. Downloadable from http://www.codeplex.com/NodeXL/Release/ProjectReleases.aspx?ReleaseId=20494
  18. Usage:
  19. .\Show-ADObject.ps1 -ADClass [(AD Type Name || Array of AD Type Names}] [-Colorize]
  20. .\Show-ADObject.ps1 -ShowADClasses
  21.  
  22.  
  23.  
  24. Example:
  25. .\Show-ADObject -ADClass group, organizationalunit -Colorize
  26.  
  27. '@
  28.         Write-Host $ExampleUsage
  29. }
  30.  
  31. #Check to see if the required files are present to run the script.
  32. function Test-Prerequisites()
  33. {
  34.         $required = @{ShowNetMap = 'Show-NodeXLMap.ps1';
  35.                 NetMapApp = 'Microsoft.NodeXL.ApplicationUtil.dll';
  36.                 NetMapControl = 'Microsoft.NodeXL.Control.dll';
  37.                 NetMapCore =  'Microsoft.NodeXL.Core.dll';
  38.                 NetMapUtil =  'Microsoft.NodeXL.Util.dll';
  39.                 NetMapVisual =  'Microsoft.NodeXL.Visualization.dll'
  40.                 }
  41.                
  42.         $report = @()
  43.        
  44.         foreach ($key in $required.Keys)
  45.         {
  46.                 $file = $required[$($key)]
  47.                 if (Test-Path $file )
  48.                 {
  49.                         Write-Debug "Found $file"
  50.                 }
  51.                 else
  52.                 {
  53.                         $report +=  "Missing $file"
  54.                 }
  55.                
  56.         }
  57.        
  58.         if ($report.count -eq 0)
  59.         {
  60.                 Write-Debug "All prerequisites were found."
  61.                 return $true
  62.         }
  63.         else
  64.         {
  65.                 Write-Help
  66.                 Write-Host "Missing files: "
  67.                 $report | ForEach-Object { Write-Host "`tMissing $_" }
  68.                 throw "Please move the needed files into the current directory!"
  69.         }
  70.  
  71.  
  72. }
  73.  
  74. #If all the prereq's are in the local directory and the script was not run
  75. #with the -help switch, load everything up!
  76. if ($NoTest -or (Test-Prerequisites))
  77. {
  78.         [Reflection.Assembly]::LoadWithPartialName("System.Drawing")   | Out-Null
  79.  
  80.         #This is just a helper function to find the parent of an object based on the parent's distinguished name
  81.         function Get-ParentFromDN()
  82.         {
  83.                 PROCESS
  84.                 {
  85.                         $root = '^DC=(\w+),DC=(\w+)$'
  86.                         $pattern = '^(OU|CN)=(\w+?),.*?DC=\w+?,DC=\w+?$'
  87.                         $dn = $_
  88.                        
  89.                        
  90.                         if ($dn -notmatch $root)
  91.                         {
  92.                                 $dn -replace $pattern, '$2'
  93.                         }
  94.                         else
  95.                         {
  96.                                 $dn -replace $root, '$1.$2'
  97.                         }
  98.                 }
  99.         }
  100.        
  101.         #This will return an array of all the Object Classes in your Active Directory
  102.         function Get-ADObjectClassName()
  103.         {
  104.                 Get-QADObject -SizeLimit 0 | Select-Object -property @{name='Name';Expression={$_.type}} -unique | Sort-Object name
  105.         }
  106.        
  107.         function Get-ColorList()
  108.         {
  109.                 [System.Drawing.Color] | Get-Member -memberType property -static | foreach {$_.name}
  110.         }
  111.        
  112.         function Get-RandomColor()
  113.         {
  114.                 param($ListOfColors)
  115.                 $count = $ListOfColors.count-1
  116.                 $rand = New-Object System.Random
  117.                 $value = $rand.next(0,$count)
  118.                 $ListOfColors[$value]
  119.         }
  120.        
  121.         function Get-ADMapObject()
  122.         {
  123.                 param($TypesToMap=$(Throw 'One (or more object types as an array) are required to run this function'), [switch]$Color)
  124.                
  125.                 $script:ColorKey = @{}
  126.                
  127.                 if ($TypesToMap -is [string])
  128.                 {
  129.                         if ($color)
  130.                         {
  131.                                 $MapColor = Get-RandomColor Get-ColorList
  132.                                 $script:ColorKey["$TypesToMap"] = $MapColor
  133.                                 Get-QADObject -Type $TypesToMap | select Name, @{name='Parent';Expression={$_.ParentContainerDN | Get-ParentFromDN}}, @{name='Color';Expression={$MapColor}}
  134.                         }
  135.                         else
  136.                         {
  137.                                 Get-QADObject -Type $TypesToMap | select Name, @{name='Parent';Expression={$_.ParentContainerDN | Get-ParentFromDN}}
  138.                         }
  139.                 }
  140.                 else
  141.                 {
  142.                         $colorList = Get-ColorList
  143.                         foreach ( $type in $TypesToMap )
  144.                         {
  145.                                 if ($color)
  146.                                 {
  147.                                         $MapColor = Get-RandomColor $colorList
  148.                                         $colorList = $colorList | ? {$_ -ne $MapColor}
  149.                                         $script:ColorKey["$type"] = $MapColor
  150.                                         Get-QADObject -Type $type | select Name, @{name='Parent';Expression={$_.ParentContainerDN | Get-ParentFromDN}}, @{name='Color';Expression={$MapColor}}
  151.                                 }
  152.                                 else
  153.                                 {
  154.                                         Get-QADObject -Type $type | select Name, @{name='Parent';Expression={$_.ParentContainerDN | Get-ParentFromDN}}
  155.                                 }
  156.                         }
  157.                 }
  158.         }
  159.        
  160.         #Helper function stolen from Doug Finke and used to create the objects to feed to
  161.         #Show-Netmap
  162.         function New-SourceTarget ($s,$t,$c)
  163.         {
  164.                 New-Object PSObject |
  165.                         Add-Member -pass noteproperty source $s |
  166.                         Add-Member -pass noteproperty target $t |
  167.                         Add-Member -pass noteproperty color $c
  168.         }
  169.        
  170.         if ($help)
  171.         {
  172.                 Write-Help
  173.         }
  174.         else
  175.         {
  176.                 if ($ShowADClass)
  177.                 {
  178.                         Get-ADObjectClassName
  179.                 }
  180.                 else
  181.                 {
  182.                         #Add the Show-Netmap functions from Doug Finke
  183.                         . .\Show-NodeXLMap.ps1
  184.                        
  185.                         if ($Colorize)
  186.                         {
  187.                                 $MapObjects = Get-ADMapObject -TypesToMap $ADClass -Color |  % { New-SourceTarget $_.Name $_.Parent $_.Color }  
  188.                                 Write-Host "Color Key: "
  189.                                 $ColorKey
  190.                         }
  191.                         else
  192.                         {
  193.                                 $MapObjects = Get-ADMapObject -TypesToMap $ADClass |  % { New-SourceTarget $_.Name $_.Parent }
  194.                         }
  195.                        
  196.                         $MapObjects | Show-NodeXLMap -layoutType $MapLayout
  197.                        
  198.                 }
  199.         }
  200.        
  201.        
  202. }

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