For this part 10 of this series, I'v made a GUI Active Directory Browser script in PowerShell.
*Warning* As you have RC2 Installed all my AD posts will not work anymore, it is verry confusing, but fixable if you read explaination here PowerShell RC2 and Active Directory :
As I did switch back on all my work PC's (I did call all my colleagues not to upgrade as all our AD tools are messed up, I will not update them, as I would not recommend to upgrade to RC2 of you work with AD a lot.
In the first post in this Active Directory series, PowerShell and Active Directory Part 1 , I showed how you could connect to the root of an Active Directory Domain,
$root = New-Object System.DirectoryServices.DirectoryEntry
Get a Sub OU :
$mowOU = $root.get_Children().find('ou=mowOu')
Connect directly to an Active Directory Object :
$mowOu = New-Object DirectoryServices.DirectoryEntry(LDAP://OU=MowOu,DC=mow,DC=local)
And how to use them on the CommandLine,
From there on, we amongst others,
Listed and created AD Objects, as OU's Users and Groups, Changed properties, Used methods, Exported and Imported/Created the Objects (Users) to and from a CSV file and did set ACL's to AD Objects.
But if you need to connect to a SubOU deep in the AD tree, it is hard to get at it this way, we or need a lot of Get_Children() and Find() Methods, or a long LDAP path.
In next post we will so how to search for Objects in Active Directory using a DirectorySearcher, and how you can connect to the Active Directory Object from the results,that also will solve part of this problem.
But most of the times would also be handy to be be able to just browse through the AD Tree to the Object you need and then use it in PowerShell to perform some actions.
I Made a Script for this in PowerShell : BrowseActiveDirectory.ps1
This Script will connect to the Root of the Domain or to custom root (a Subcontainer supplied as a DirectoryEntry Object, if you did follow the rest of the series, I hope this makes perfect sense ;-) ) and if this succeeds builds a Form that contains a TreeView Object, that you can use to browse to the AD object you need, and then Returns it so you can use it in PowerShell.
This Form, (as you might have seen in my Teaser), Looks like this (click picture to enlarge)
In this case I walked to the OU : MowOU, I also used in former examples, when the Active Directory Browser will Startup you will only see the Root Object with the DN of the AD Object it represents.
for performance reasons, not the Whole AD-tree will be read when starting up, but only when you select a Node in the TreeView, the Children of the AD object it presents get enumerated and there DN's are added to the TreeView, we will use events from the TreeView to do this.
When you use the Select Button or hit Enter, the DirectoryEntry will be retrieved and Passed back to the pipeline so you can Catch it in the PowerShell console and put it into a variable for further use.
I like this GUI form as a quick way to get to the Object I want in AD, it is much quicker then getting there from the commandline, or it also much easier to explore and look at the structure of the ActiveDirectory domain this way.
As already mentioned in PowerShell and Active Directory Part 8 (ACL's) , I like to just use "loose lines of Code" or functions, that you just can just past into the Commandline, so most of my examples are given that way
And I gave some more examples and explained a bit more about the difference with Scripts here :PowerShell, Has my Dell a dangerous battery ? ,
This script I made Hybrid, you can start it directly or start it to load the function, I did add this function as a Switch Parameter,
so Next to Using the script Directly like this :
PoSH>$De = .\ActiveDirectoryBrowser.Ps1
PoSH>$De = fl *
PoSH>$De = .\ActiveDirectoryBrowser.Ps1
PoSH>$De | fl *objectClass : {top, organizationalUnit}
ou : {MowSubOu}
distinguishedName : {OU=MowSubOu,OU=MowOu,DC=mow,DC=local}
instanceType : {4}
whenCreated : {6/26/2006 6:59:59 PM}
whenChanged : {6/26/2006 6:59:59 PM}
uSNCreated : {System.__ComObject}
uSNChanged : {System.__ComObject}
name : {MowSubOu}
objectGUID : {162 13 61 122 139 39 201 72 161 216 129 101 53 217 180 114}
objectCategory : {CN=Organizational-Unit,CN=Schema,CN=Configuration,DC=mow,DC=local}
nTSecurityDescriptor : {System.__ComObject}# Use this as new Root
PoSH>.\ActiveDirectoryBrowser.Ps1 $de
You can also use the Browse-ActiveDirectory Function that does all the work in the Script, by Loading it into your current session by dotSourcing the ActiveDirectoryBrowser.Ps1 script and providing the -loadOnly Switch
then you can use the Browse-ActiveDirectory function interactively from then on, so you can also use this script in your profile
If you use the -LoadOnly Switch but do not DotSource it the script will warn you like this :
PoSH>.\ActiveDirectoryBrowser.Ps1 -l
WARNING: LoadOnly Switch is given but you also need to 'dotsource' the script to load the function in the global scope
To Start a script in the global scope (DotSource) put a dot and a space in front of path to the script
If the script is in the current directory this would look like this :
. .\ActiveDirectoryBrowser.Ps1
then :
The Browse-ActiveDirectory Function is loaded and can be used like this :
$de = Browse-ActiveDirectory
PoSH>. .\ActiveDirectoryBrowser.Ps1 -l
The Browse-ActiveDirectory Function is loaded and can be used like this :
$de = Browse-ActiveDirectory
If you use the script this way for loading the function, it also will Define the alias Bad for ease of use
I hope this LoadOnly option example also shows a bit how you can use the different forms of starting things in PowerShell.
The Script looks like this :
Most of this Code is straight forward I think with the # comments( if you followed the AD series, and have seen some of my other PowerShell GUI Examples) , just remember that the add_* methods are used to add scripts to events from the objects on the Form.
here some remark by the main points of interest in the script (sorry Numbers Gone) :
Line 24 : &{trap {throw "$($_)"};[void]$Root.get_Name()}
This line does test the ActiveDirectory Connection by getting the name Property and use a Trap block to catch it and throwing the Error again so the script will stop if this fails.
Line 49,64 :$btnCancel.add_Click({$script:Return = $false ; $form.close()})
The add_Click methods handle the event when I button gets clicked you can give this funtion a Delegate ScriptBlock that gets executed every time the button get clicked.
Line 75-95 : $TV.add_AfterSelect({
This whole Scriptblock is also an eventhandler delegate, this one from the Treeview object and it is called every time a TreeNode is selected,
you can see there is a variable with the name $This, this will contain the TreeView object so we can get the treenode that is selected at that moment from that
Line 76 : if ($this.SelectedNode.tag -eq "NotEnumerated")
we check if we already did get the children of this node, otherwise we will collect them and add SubNodes to the TreeView for them.
Line 109 : $form.AcceptButton = $btnSelect
Make The Select Button the Default so it will get a click event when [Enter] is pressed
Line 111 : $Form.Add_Shown({$form.Activate()})
Used to give the form focus
Line 120 : The logic to make the script "Hybrid"
For more info about the AD parts see the rest of the series,
For more info about building GUI's and placing and using Form elements on them in PowerShell See my Other GUI examples in former posts.
Using a DataGrid :
PowerShell out-DataGrid update and more DataSet utilities
Using a Property Grid (Object Viewer)
PowerShell-out-propertygrid-msh-view.html
about Focus the Form problem (updated version this script)
Simple Show-Image function for Monad
Adding menus etc ,
MSH Concentration (scripting games part 4)
Hosting PowerShell in it.
Hosting an MSH runspace from Monad
Use For EventWatcher workaround (STA Thread ), and how to use Popup balloons.
MSH directory watcher with popup-balloon
There are a bit more posts to find, if you search on GUI on my blog.
*Edit* as the lineNumbers do stay if you copy the Code Above and pasting it, So I'm posting the code another time without LineNumbers
Enjoy,
greetings /\/\o\/\/
Tags : Monad msh PowerShell
October 2005 November 2005 December 2005 January 2006 February 2006 March 2006 April 2006 May 2006 June 2006 July 2006 August 2006 September 2006 October 2006 November 2006 December 2006