Reflection Can';无法在PowerShell中加载.NET类型

Reflection Can';无法在PowerShell中加载.NET类型,reflection,powershell,assemblies,Reflection,Powershell,Assemblies,这是一个奇怪的。我正在尝试加载System.DirectoryServices程序集,然后创建System.DirectoryServices.DirectoryEntry类的实例 以下是我想做的: PS C:> [System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices") GAC Version Location --- ------- --------

这是一个奇怪的。我正在尝试加载
System.DirectoryServices
程序集,然后创建
System.DirectoryServices.DirectoryEntry
类的实例

以下是我想做的:

PS C:> [System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices")

GAC    Version        Location
---    -------        --------
True   v2.0.50727     C:\Windows\assembly\GAC_MSIL\System.DirectoryServices\2.0.0.0__b03f5f7f11d50a3a\System.Directo...
它似乎很好地加载了程序集,但现在当我尝试实例化一个新对象时,它失败了:

PS C:\> $computer = new-object [System.DirectoryServices.DirectoryEntry]("WinNT://localhost,computer")
New-Object : Cannot find type [[System.DirectoryServices.DirectoryEntry]]: make sure the assembly containing this type
is loaded.
At line:1 char:23
+ $computer = new-object <<<<  [System.DirectoryServices.DirectoryEntry]("WinNT://localhost,computer")
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
它显示我已成功创建对象:

distinguishedName :
Path              : WinNT://localhost,computer
正确的方法是什么?我做错了什么?

你的语法有点不正确。试试这个:

[System.Reflection.Assembly]:LoadWithPartialName(“System.DirectoryServices”)
$machine=new object-typeName System.DirectoryServices.DirectoryEntry-argumentList“WinNT://localhost,computer”
我认为的语法不正确。摘自[关于新对象的一些文档]:


不需要加载任何内容。使用adsi型加速器:

[adsi]"WinNT://localhost,computer"

是的,在PowerShell中使用C风格的“构造函数”在技术上是无效的。按预期方式在New Object cmdlet上使用-TypeName和-ArgumentList参数。有什么好的文档吗?事情似乎不太“可发现”,请搜索powershell类型的加速器
New-Object System.DirectoryServices.DirectoryEntry("WinNT://localhost,computer")
[adsi]"WinNT://localhost,computer"