7

Using the System.DirectoryServices.AccountManagement namespace, PrincipalContext class in PowerShell. I am unable to call PrincipalContext Constructor (ContextType, String) via

[System.DirectoryServices.AccountManagement.PrincipalContext]::PrincipalContext($ContextType, $ContextName)

I get error "Method invocation failed because [System.DirectoryServices.AccountManagement.PrincipalContext] does not contain a method named 'PrincipalContext'."

Can it only be called the following way?

New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList $ContextType, $ContextName

Would like to understand why it works the second way but not the first way. Is there a way to do this with square brackets?

Full code is this:

Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$ContextName = $env:COMPUTERNAME
$ContextType = [System.DirectoryServices.AccountManagement.ContextType]::Machine
$PrincipalContext = [System.DirectoryServices.AccountManagement.PrincipalContext]::PrincipalContext($ContextType, $ContextName)
$IdentityType = [System.DirectoryServices.AccountManagement.IdentityType]::SamAccountName
[System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($PrincipalContext, $IdentityType, 'Administrators')

1 Answer 1

6

Using the double colon after a .net class is used to call a static method on that class.

See: Using Static Classes and Methods

Using the below syntax:

[System.DirectoryServices.AccountManagement.PrincipalContext]::PrincipalContext($ContextType, $ContextName)

You are trying to call a static method named PrincipalContext on the PrincipalContext class instead of the constructor.

Can it only be called the following way?

AFAIK, you need to create the instance of the class (call the constructor) using the New-Object cmdlet.

Would like to understand why it works the second way but not the first way. Is there a way to do this with square brackets?

It works the second way because you are correctly creating a new object and calling the constructor. It doesn't work the first way because you are not calling the constructor - you are attempting to call a static method.

Sign up to request clarification or add additional context in comments.

3 Comments

Please dumb it down. I don't really know what that should mean to me in this context.
@Entbark - I expanded my original answer.
For those finding this, in Powershell v5.1 (maybe even 5.0 - but not before) classes have the constructors available from the square brackets notation.This alows Powershell ISE to use Intellisense for the constructor signatures. Called like so: [System.DirectoryServices.AccountManagement.PrincipalContext]::new($ContextType, $ContextName).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.