在PowerShell中使用System.DirectoryServices.AccountManagement命名空间,PrincipalContext类。我无法通过在PowerShell中调用构造函数的.NET语法
[System.DirectoryServices.AccountManagement.PrincipalContext]::PrincipalContext($ContextType, $ContextName)
我得到错误调用PrincipalContext Constructor (ContextType, String)
"Method invocation failed because [System.DirectoryServices.AccountManagement.PrincipalContext] does not contain a method named 'PrincipalContext'.
“
可它只能叫下面的方式?
New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList $ContextType, $ContextName
想明白为什么它的第二个方式,但不是第一种方法有没有办法用方括号做这个?
完整代码是这样的:
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')
请放下它。在这种情况下,我不知道这对我意味着什么。 –
@Entbark - 我扩大了我的原始答案。 – dugas
对于那些发现这种情况的人来说,在Powershell v5.1(甚至可能是5.0--但不是之前)类中,可以使用方括号表示法中的构造函数。这使Powershell ISE可以使用Intellisense作为构造函数签名。像这样调用:'[System.DirectoryServices.AccountManagement.PrincipalContext] :: new($ ContextType,$ ContextName)''。 –