2012-03-22 28 views
0

我是.NET和Powershell的新手程序员,powershell如何从外部加载的DLL返回类型/对象?

我有一个小的编译.NET DLL,它利用目录服务和TSUserExLib获取TerminalService属性。该DLL有一个返回“IADsTSUserEx”的静态函数。我测试了DLL,它在返回字符串时有效,但是可以将IADsTSUserEx类\ object类型返回给powershell吗?当我从powershell执行静态函数时,我什么也没有回来,甚至没有空。我试了一下使用以下命令

Add-Type -Path "c:\temp\test.dll" 
[ABC.Class1]::getTSEntry("[email protected]") 

这个DLL中包含此代码段:

DirectoryEntry user = result.GetDirectoryEntry(); 
       IADsTSUserEx tsuser = (IADsTSUserEx)result.GetDirectoryEntry().NativeObject; 
       return tsuser; 
+1

尝试运行'[ABC.Class1] :: getTSEntry( “[email protected]”)| Get-Member'并查看显示内容。我猜并不是'getTSEntry'返回'$ null',而是PowerShell不知道如何格式化和显示'IADsTSUserEx'对象。 – BACON 2012-03-22 21:57:29

+0

@BACON,谢谢你的回应!我尝试了你的建议,并得到以下错误:“Get-Member:没有指定对象到get-member cmdlet”。我知道这个调用正在执行,因为它会触发不存在的用户的异常。似乎它只是不知道如何处理返回对象,所以它不会返回任何内容,甚至不会返回null。即使是“[ABC.Class1] :: getTSEntry(”[email protected]“)-eq $ null”语句什么都没有 – 2012-03-23 14:44:54

+0

无法编辑以前的评论,但也想声明“[ABC.Class1] :: getTSPath(”[email protected]“)。getType()返回一个BaseType的”System.MarshalByRefObject“和Name的” __ComObject“ – 2012-03-23 14:52:41

回答

0

因为你的方法返回一个COM对象和PowerShell不直接暴露其方法和属性给你,你“会需要反射来访问他们和Type.InvokeMember method

$entry = [ABC.Class1]::getTSEntry("[email protected]"); 
$entryType = $entry.GetType(); 
$binder = $null; 
$someMethodParameters = @('Parameter #1', 12345, 'Parameter #3'); 
$someMethodResult = $entryType.InvokeMember('SomeMethod', 'Public, Instance, InvokeMethod', $binder, $entry, $someMethodParameters); 
$somePropertyValue = $entryType.InvokeMember('SomeProperty', 'Public, Instance, GetProperty', $binder, $entry, $null); 
+0

感谢您的回应。我尝试了您的建议,但是我得到一个错误”已经从它的底层RCW中分离出来的COM对象无法使用“。我假设这意味着DirectoryEntry.NativeObject。可能是某些被丢弃的对象从DLL的回调可能?无论如何,我只会做错误的方式,并翻译每个属性返回一个字符串,而不是返回整个TSUserEX类。 – 2012-03-23 20:39:16