2012-05-24 17 views
6

これは奇妙なものです。 System.DirectoryServicesアセンブリを読み込み、System.DirectoryServices.DirectoryEntryクラスのインスタンスを作成しようとしています。PowerShellで.NETタイプを読み込めません。

は、ここで私は何をしようとしているのです。私はそれが失敗した新しいオブジェクトをインスタンス化しようとすると、

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 

私がやや鈍いやり方でやろうとすると、うまくいくと思われます。

$directoryServices = [System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices") 
$directoryEntryType = $directoryServices.GetType("System.DirectoryServices.DirectoryEntry") 
$machine = New-Object $directoryEntryType("WinNT://localhost,computer") 
$machine 

それは私が成功したオブジェクトを作成した私を示しています

distinguishedName : 
Path    : WinNT://localhost,computer 

これを行うための適切な方法は何ですか?私は間違って何をしていますか?

答えて

7

new-objectの構文は少しです。これを試してください:

[System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices") 
$machine = new-object -typeName System.DirectoryServices.DirectoryEntry -argumentList "WinNT://localhost,computer" 
+0

ええ、C#スタイルの「コンストラクタ」は、PowerShellでは技術的に無効です。 New-Objectコマンドレットの-TypeNameパラメーターと-ArgumentListパラメーターは、意図したとおりに使用してください。 –

0

私はNew-Objectの構文が間違っていると思います。 [新オブジェクト上のいくつかのドキュメント]から撮影:1

New-Object System.DirectoryServices.DirectoryEntry("WinNT://localhost,computer") 
5

何かをロードする必要はありません。 adsiタイプのアクセラレータを使用してください:

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

良い文書がありますか?物事は "発見できない"と思われません – Micah

+0

powershell type accelartors –

+0

+1を検索してください。これが「正しい」答えです。 – vcsjones

関連する問題