Today, I am going to show how we can leverage the power of Powershell V2 and Active Directory Module, and easily write powerful, professional cmdlets (with help message, positional parameters, default prompting, error handling etc.) for AD.
Couple of weeks ago, one of my colleagues (Alexander Lash) introduced me to a cool feature in Powershell V2 called Advanced Functions, using which one can write full-blown cmdlets within a module natively in Powershell! I thought of experimenting with this feature a little bit and build some cmdlets that would extend the current functionality of AD cmdlets. The outcome of my experimentation is a new module called ActiveDirectoryExtension in which I and some of my team members are planning to add utility functions/cmdlets built using Active Directory Module cmdlets.
The first set of the cmdlets that I am publishing today is an extension of Get-ADUser, Get-ADGroup cmdlets that would support NT4 style account names (domain\username) and UPNs (UserPrincipalName ex: username@domain.com) as identity.
Here is some sample usage using my new set of cmdlets. I have prefixed all the nouns with “XAD” meaning eXtending AD.
Loading ActiveDirectoryExtension module
Run ActiveDirectoryExtensions.ps1 in a Powershell Console and import ActiveDirectoryExtensions module. Scroll below for complete script and download instructions.
PS C:\> C:\ActiveDirectoryExtension.ps1PS C:\> import-module ActiveDirectory
Tips: Copy the above commands into your $PROFILE file, so that both ActiveDirectory and ActiveDirectoryExtension will always be loaded when you open a new Powershell console.
Sample commands using the new extension cmdlets
PS C:\> Get-XADUser administratorPS C:\> Get-XADUser fabrikam\administrator ## NT4 Style name. Should work for cross forest and cross domain scenarios too.PS C:\> Get-XDAUser administrator@fabrikam.com ## UPN. NOTE: UserPrincipalName must be set on the account for this to work. ## Should work for cross domain scenarios. PS C:\> Get-XADComputer myMemberServer * ## Fetches all the attributes set on the computer object with identity myMemberServer.PS C:\> Get-XADComputer myMemberServer * | Out-SortADProperties ## Sorts and displays all the properties found in the object as string. PS C:\> Get-PossibleLdapAttributes user ## Prints out all the Ldap Attributes that can be set on a user objectPS C:\> Get-PossibleLdapAttributes computer ## Prints out all the Ldap Attributes that can be set on a computer objectPS C:\> Get-PossibleLdapAttributes group ## Prints out all the Ldap Attributes that can be set on a group objectPS C:\> Get-PossibleLdapAttributes msDS-ManagedServiceAccount ## Prints out all the Ldap Attributes that can be set on a service account object
PS C:\> Get-XADUser administratorPS C:\> Get-XADUser fabrikam\administrator ## NT4 Style name. Should work for cross forest and cross domain scenarios too.PS C:\> Get-XDAUser administrator@fabrikam.com ## UPN. NOTE: UserPrincipalName must be set on the account for this to work. ## Should work for cross domain scenarios.
PS C:\> Get-XADComputer myMemberServer * ## Fetches all the attributes set on the computer object with identity myMemberServer.PS C:\> Get-XADComputer myMemberServer * | Out-SortADProperties ## Sorts and displays all the properties found in the object as string.
PS C:\> Get-PossibleLdapAttributes user ## Prints out all the Ldap Attributes that can be set on a user objectPS C:\> Get-PossibleLdapAttributes computer ## Prints out all the Ldap Attributes that can be set on a computer objectPS C:\> Get-PossibleLdapAttributes group ## Prints out all the Ldap Attributes that can be set on a group objectPS C:\> Get-PossibleLdapAttributes msDS-ManagedServiceAccount ## Prints out all the Ldap Attributes that can be set on a service account object
Script
Here is the complete script:
NOTE: This script is just for proof-of-concept and is not intended to be production quality.
Update: Brandon Shell pointed out that Powershell has dropped the term "Script Cmdlets" and instead call them as "Advanced Functions". I have updated my post accordingly. He also pointed out few improvements to the script in terms of performance and brevity. I have updated the script with those changes. Thanks Brandon!
Cheers!Swami
--Swaminathan Pattabiraman [MSFT]Developer – Active Directory Powershell Team