widsnet.com
11Jul/120

Get AD Account Properties without using ActiveDirectory cmdlets

Sometimes it's not possible or not preferred to use the Active Directory commandlets supplied by MS. Then it's lucky that the same tasks can be performed without them.

function Get-ADAccountProperties {
    Param(
        [parameter(Mandatory = $true,position=0,valueFromPipeline=$true)]$Account,
        [switch]$UnderlyingObjects
    )
	BEGIN {
	    Add-Type -AssemblyName System.DirectoryServices.AccountManagement
        $ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
	}
    PROCESS {
		If (-not $PSBOUNDPARAMETERS.ContainsKey('Account')) {
			$Account = $Input
		}
        $AccountObject = [System.DirectoryServices.AccountManagement.Principal]::FindByIdentity($ct,$Account)
        If ($UnderlyingObjects) {
            Return $AccountObject.GetUnderlyingObject().Properties
        }
        else {
            Return $AccountObject
        }
    }
}

Get-ADAccountProperties $env:USERNAME
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.