Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

PowerShell Community Extensions Active Directory Provider Part 1

In my presentations with Bruce Payette at the Developer IT Pro Days in Belgium we did show a lot if community resources , Bruce did show the Gac Provider and some utilities from the Community Extension project on CodePlex in both of the sessions and In the March 29, 2006 Thinking PowerShell  session I did show the PSCX project page on Codeplex , and I did a quick demo of the PowerShell Community Extensions 1.1 (PSCX 1.1) Active directory provider. 

In the Chalk and Talk session the day before I had some troubles with my "To Old" VM hosting my Demo Domain, (No keyboard) so was not able to show it there, but this time it all did go OK , as I had problems again with the VM in the session before my part started I was glad I was able to do demo pfff ;-) )

In this blogpost I will show the Demo I did there and I will also do a  PowerShell Community ExtensionsActive Directory Provider Part about using the PSCX 1.1 AD provider on my blog in next post  where I will add some extra usage information, by showing to select a user using the PSCX AD provider and change the description, that was not in the session but from a private discussion and demo I had after the session.

Two extra providers added by PSCX 1.1

In the session Bruce Payette and I did show that if you load the PowerShell Community Extensions 1.1 SnapIn (PSCX) , and you look at your drives you can see that 2 drives are added. 

 

PoSH> Get-PSDrive                                                                                                       
                                                                                                                        
Name       Provider      Root                                                                           CurrentLocation 
----       --------      ----                                                                           --------------- 
A          FileSystem    A:\                                                                                            
Alias      Alias                                                                                                        
C          FileSystem    C:\                                                                                 PowerShell 
cert       Certificate   \                                                                                              
D          FileSystem    D:\                                                                                            
Env        Environment                                                                                                  
Function   Function                                                                                                     
HKCU       Registry      HKEY_CURRENT_USER                                                                              
HKLM       Registry      HKEY_LOCAL_MACHINE                                                                             
Variable   Variable                                                                                                     
Z          FileSystem    Z:\                                                                                            
                                                                                                                        
                                                                                                                        
PoSH> Add-PSSnapin PSCX                                                                                                 
PoSH> Get-PSDrive | ft -AutoSize                                                                                        
                                                                                                                        
Name     Provider          Root               CurrentLocation                                                           
----     --------          ----               ---------------                                                           
A        FileSystem        A:\                                                                                          
Alias    Alias                                                                                                          
C        FileSystem        C:\                     PowerShell                                                           
cert     Certificate       \                                                                                            
D        FileSystem        D:\                                                                                          
Env      Environment                                                                                                    
Function Function                                                                                                       
Gac      AssemblyCache     Gac                                                                                          
HKCU     Registry          HKEY_CURRENT_USER                                                                            
HKLM     Registry          HKEY_LOCAL_MACHINE                                                                           
MOW      DirectoryServices mow.local\                                                                                   
Variable Variable                                                                                                       
Z        FileSystem        Z:\                                                                                          
                                                                                                                        
                                                                                                                        
PoSH>                                                                                 

 a Gac: Drive providing access to the AssemblyCache that Bruce did show in the session, and a drive with the name of your Domain, in my case MOW: , providing access to Active directory, the Demo I did and am posting now.This is because PSCX 1.1 does contain a AssemblyCache and an Active Directory provider

Navigating in Active Directory using the Provider

In my demo I did show that that enables us to CD into the Active directory "PSDrive", and do a LS (Dir or get-ChildItem)

 

PoSH> cd mow:                                                                                                           
PoSH> pwd                                                                                                               
                                                                                                                        
Path                                                                                                                    
----                                                                                                                    
MOW:\                                                                                                                   
                                                                                                                        
                                                                                                                        
PoSH> pwd | Format-List *                                                                                               
                                                                                                                        
                                                                                                                        
Drive        : MOW                                                                                                      
Provider     : PSCX\DirectoryServices                                                                                   
ProviderPath : mow.local\                                                                                               
Path         : MOW:\                                                                                                    
                                                                                                                        
                                                                                                                        
                                                                                                                        
PoSH> ls                                                                                                                
                                                                                                                        
         LastWriteTime Type                 Name                                                                        
         ------------- ----                 ----                                                                        
    5/18/2006  6:54 PM builtinDomain        Builtin                                                                     
    5/18/2006  6:54 PM container            Computers                                                                   
    5/18/2006  6:54 PM organizationalUnit   Domain Controllers                                                          
    9/13/2006  9:00 PM group                foo                                                                         
    9/29/2006  5:53 PM user                 FooMoved                                                                    
    5/18/2006  6:54 PM container            ForeignSecurityPrincipals                                                   
    5/18/2006  6:54 PM infrastructureUpdate Infrastructure                                                              
    9/27/2006 10:13 PM user                 Ken Myer                                                                    
    5/18/2006  6:54 PM lostAndFound         LostAndFound                                                                
     8/2/2006  9:52 PM organizationalUnit   MowOtherOU                                                                  
    5/18/2006  9:15 PM organizationalUnit   MowOu                                                                       
    8/15/2006  8:12 PM user                 NewUs:::er0003                                                              
    8/14/2006 10:26 PM user                 NewUser0010                                                                 
    8/15/2006  8:12 PM user                 NewUser0011                                                                 
    8/15/2006  8:12 PM user                 NewUser0012                                                                 
    5/18/2006  6:54 PM msDS-QuotaContainer  NTDS Quotas                                                                 
    5/18/2006  6:54 PM container            Program Data                                                                
    5/18/2006  6:54 PM container            System                                                                      
   11/15/2006 12:13 PM user                 test                                                                        
    5/18/2006  6:54 PM container            Users                                                                       
                                                                                                                        
                                                                                                                        
PoSH>                                                                               

 

Recursion and Filtering 

We also can filter on a type (take care this is a multi valued property, see output below), or do a recursive listing :

 

PoSH> cd MowOu                                                                                                          
PoSH> pwd                                                                                                               
                                                                                                                        
Path                                                                                                                    
----                                                                                                                    
MOW:\MowOu                                                                                                              
                                                                                                                        
                                                                                                                        
PoSH> ls                                                                                                                
                                                                                                                        
         LastWriteTime Type                 Name                                                                        
         ------------- ----                 ----                                                                        
    6/12/2006  3:43 PM user                 $_                                                                          
    6/28/2006 11:41 PM user                 foo                                                                         
    8/25/2006  1:40 AM user                 foobar                                                                      
    7/19/2006 10:57 PM user                 Ken Myer                                                                    
    9/12/2006  7:08 PM user                 mow                                                                         
    6/26/2006  8:57 PM user                 Mow2                                                                        
    6/26/2006  8:59 PM organizationalUnit   MowSubOu                                                                    
    9/24/2006 12:11 AM user                 MyerKen                                                                     
    8/12/2006  5:06 PM user                 NewUser0003                                                                 
    8/12/2006  4:19 AM user                 NewUser0010                                                                 
    8/12/2006  4:54 AM user                 NewUser0011                                                                 
    8/12/2006  2:02 AM user                 NewUser0267                                                                 
    9/13/2006  9:32 PM group                TestGroup                                                                   
                                                                                                                        
                                                                                                                        
PoSH> ls |? {$_.type -eq 'group'}                                                                                       
PoSH> ls |? {$_.type -match 'group'}                                                                                    
                                                                                                                        
         LastWriteTime Type                 Name                                                                        
         ------------- ----                 ----                                                                        
    9/13/2006  9:32 PM group                TestGroup                                                                   
                                                                                                                        
                                                                                                                        
PoSH> ( gi TestGroup ).Type                                                                                             
                                                                                                                        
Name                                                                                                        IsContainer 
----                                                                                                        ----------- 
group                                                                                                             False 
                                                                                                                        
                                                                                                                        
PoSH> ls |? {$_.type.name -eq 'group'}                                                                                  
                                                                                                                        
         LastWriteTime Type                 Name                                                                        
         ------------- ----                 ----                                                                        
    9/13/2006  9:32 PM group                TestGroup                                                                   
                                                                                                                        
                                                                                                                        
PoSH> cd ..                                                                                                             
PoSH> ls -Rec |? {$_.type.name -eq 'organizationalUnit'}                                                                
                                                                                                                        
         LastWriteTime Type                 Name                                                                        
         ------------- ----                 ----                                                                        
    5/18/2006  6:54 PM organizationalUnit   Domain Controllers                                                          
     8/2/2006  9:52 PM organizationalUnit   MowOtherOU                                                                  
    5/18/2006  9:15 PM organizationalUnit   MowOu                                                                       
    6/26/2006  8:59 PM organizationalUnit   MowSubOu                                                                    
                                                                                                                        
                                                                                                                        
PoSH> ls -Rec |? {$_.type.name -eq 'organizationalUnit'} | fl *                                                         
                                                                                                                        
                                                                                                                        
PSPath            : PSCX\DirectoryServices::MOW:\Domain Controllers                                                     
PSParentPath      : PSCX\DirectoryServices::MOW:                                                                        
PSChildName       : Domain Controllers                                                                                  
PSDrive           : MOW                                                                                                 
PSProvider        : PSCX\DirectoryServices                                                                              
PSIsContainer     : True                                                                                                
Type              : organizationalUnit                                                                                  
Name              : Domain Controllers                                                                                  
Description       : Default container for domain controllers                                                            
LastWriteTime     : 5/18/2006 6:54:57 PM                                                                                
FullName          : MOW:\Domain Controllers                                                                             
CanonicalName     : mow.local/Domain Controllers                                                                        
DistinguishedName : OU=Domain Controllers,DC=mow,DC=local                                                               
Types             : {top, organizationalUnit}                                                                           
Entry             : System.DirectoryServices.DirectoryEntry                                                             
IsContainer       : True                                                                                                
                                                                                                                        
PSPath            : PSCX\DirectoryServices::MOW:\MowOtherOU                                                             
PSParentPath      : PSCX\DirectoryServices::MOW:                                                                        
PSChildName       : MowOtherOU                                                                                          
PSDrive           : MOW                                                                                                 
PSProvider        : PSCX\DirectoryServices                                                                              
PSIsContainer     : True                                                                                                
Type              : organizationalUnit                                                                                  
Name              : MowOtherOU                                                                                          
Description       :                                                                                                     
LastWriteTime     : 8/2/2006 9:52:50 PM                                                                                 
FullName          : MOW:\MowOtherOU                                                                                     
CanonicalName     : mow.local/MowOtherOU                                                                                
DistinguishedName : OU=MowOtherOU,DC=mow,DC=local                                                                       
Types             : {top, organizationalUnit}                                                                           
Entry             : System.DirectoryServices.DirectoryEntry                                                             
IsContainer       : True                                                                                                
                                                                                                                        
PSPath            : PSCX\DirectoryServices::MOW:\MowOu                                                                  
PSParentPath      : PSCX\DirectoryServices::MOW:                                                                        
PSChildName       : MowOu                                                                                               
PSDrive           : MOW                                                                                                 
PSProvider        : PSCX\DirectoryServices                                                                              
PSIsContainer     : True                                                                                                
Type              : organizationalUnit                                                                                  
Name              : MowOu                                                                                               
Description       :                                                                                                     
LastWriteTime     : 5/18/2006 9:15:13 PM                                                                                
FullName          : MOW:\MowOu                                                                                          
CanonicalName     : mow.local/MowOu                                                                                     
DistinguishedName : OU=MowOu,DC=mow,DC=local                                                                            
Types             : {top, organizationalUnit}                                                                           
Entry             : System.DirectoryServices.DirectoryEntry                                                             
IsContainer       : True                                                                                                
                                                                                                                        
PSPath            : PSCX\DirectoryServices::MOW:\MowOu\MowSubOu                                                         
PSParentPath      : PSCX\DirectoryServices::MOW:\MowOu                                                                  
PSChildName       : MowSubOu                                                                                            
PSDrive           : MOW                                                                                                 
PSProvider        : PSCX\DirectoryServices                                                                              
PSIsContainer     : True                                                                                                
Type              : organizationalUnit                                                                                  
Name              : MowSubOu                                                                                            
Description       :                                                                                                     
LastWriteTime     : 6/26/2006 8:59:59 PM                                                                                
FullName          : MOW:\MowOu\MowSubOu                                                                                 
CanonicalName     : mow.local/MowOu/MowSubOu                                                                            
DistinguishedName : OU=MowSubOu,OU=MowOu,DC=mow,DC=local                                                                
Types             : {top, organizationalUnit}                                                                           
Entry             : System.DirectoryServices.DirectoryEntry                                                             
IsContainer       : True                                                                                                
                                                                                                                        
                                                                                                                        
                                                                                                                        
PoSH>                                                                               

 As I had only little time in the session this was all that I was able to demo, still I think this allready shows how cool this provider is,

Accesing and Changing Active Directory Objects

but after the session I also got a question about how to change a property on an Active Directory object.

As this was a very interesting topic, I will demo this in the next post,

As you worked with AD before in PowerShell without using the provider you might have noticed  from the examples given that the output about the Active directory Objects is slightly different as when used directly, this is because the PSCX Active Directory Provider does use wrapper objects for this, so we have to take that in account when accessing the objects using the provider, but more about that in next post.

 

Enjoy,

 

Greetings /\/\o\/\/

Published Friday, April 06, 2007 6:30 PM by admin

Comments

# Best Posts from around the Web » PowerShell Community Extensions Active Directory Provider Part 1

# re: PowerShell Community Extensions Active Directory Provider Part 1

This is awesome.  I'm anxiously awaiting your next post. :)

Saturday, April 07, 2007 6:52 PM by Robbie Foust

# PowerShell Community Extensions Active Directory Provider Part 2

In part one of this series : PowerShell Community Extensions Active Directory Provider Part 1 I did show

Tuesday, April 10, 2007 2:13 PM by The PowerShell Guy

# Active Directory Cmdlets

More great cmdlets from our partners. Quest Software , known for its ActiveRoles Server and PowerGUI

Friday, April 13, 2007 12:27 PM by Windows PowerShell

# Adminspotting.net » Links for April 15, 2007

Sunday, April 15, 2007 10:00 PM by Adminspotting.net » Links for April 15, 2007

# re: PowerShell Community Extensions Active Directory Provider Part 1

Ii was wondering if there is a script or how to write one that would monitor a list of LanID's for lockouts then email and txt message when those users become locked out.  

any suggestions or help would be greatly appreciated.

dearitts@hotmail.com

Monday, April 16, 2007 12:48 PM by Dean

# PowerShell Community Extensions

PowerShell Community Extensions är ett öppet projekt som skapar tillägg till PowerShell, cmdlets, aliases,...

Thursday, June 14, 2007 5:38 AM by Fredrik Wall [Mycket skript blir det]

# re: PowerShell Community Extensions Active Directory Provider Part 1

I have a problem where particular user properties can be viewed via adsi edit but are not displayed by Powershell - Powershell returns a name or a cn but when I try to return for example pwdLastSet or createTimeStamp for users then no data is returned. I am running .NET 2.0

I have some sample script below which homes in on the problem I am having - any advice would be muchly appeciated as I have started using PS and like it but if I can't get this working I'm going to have to revert to VBScript which would be a shame as it would mean me losing faith in PS at this early stage:

#

$Dom = 'LDAP://ou=SWAPCLEARITD_Test,dc=X,dc=X'

$Root = New-Object DirectoryServices.DirectoryEntry $Dom cls Write-host "PowerShell connects to domain: $Dom `n"

# Create a selector and start searching from the Root of AD $selector = New-Object DirectoryServices.DirectorySearcher

$selector.SearchRoot = $root

# Filter the users with -like "CN=*". Note the ForEach loop $adobj=

$selector.findall() `

| where {$_.properties.objectcategory -like "CN=*"}

foreach ($Group in $adobj){

$prop=$Group.properties

Write-host "$($prop.name)"

}

Write-host "`n`n"

Write-host "There are $($adobj.count) Groups in the $($root.name) OU."

##

######EndOfScript

Wednesday, October 24, 2007 3:21 AM by Jobbsy
Anonymous comments are disabled