2012-03-22 24 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