これは奇妙なものです。 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
これを行うための適切な方法は何ですか?私は間違って何をしていますか?
ええ、C#スタイルの「コンストラクタ」は、PowerShellでは技術的に無効です。 New-Objectコマンドレットの-TypeNameパラメーターと-ArgumentListパラメーターは、意図したとおりに使用してください。 –