2016-02-29 99 views
0

我需要做到這一點使用.Net的3.5無法調用System.Management.Automation.Runspaces.WSManConnectionInfo

我已經引用我的項目中所需的DLL:

[enter image description here]

using System.Management.Automation; 
using System.Management.Automation.Runspaces; 

但每當我嘗試使用WSManConnectionInfo,例如here

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(); 
connectionInfo.ComputerName = machineAddress; 
Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo); 
runspace.Open(); 
using (PowerShell ps = PowerShell.Create()) 
{ 
    ps.Runspace = runspace; 
    ps.AddScript("Get-Service"); 
    var results = ps.Invoke(); 
    // Do something with result ... 
} 
runspace.Close(); 

我得到一個「類型找不到」錯誤:

The type or namespace 'WSManConnectionInfo' could not be foun (are you missing a using directive or assembly reference?)

我試圖遠程連接到我們的服務器上的PowerShell會話(從客戶端計算機上本地運行的應用程序),它然後可以查詢哪些進程正在該人員的會話中運行。

根據MSDN所有我需要的是​​名稱空間,我看不到任何提及的框架版本限制。我在這裏真的很愚蠢,還是微軟在宇宙中只有最糟糕的文檔?

+0

Microsoft.CSharp用感嘆號,有趣... – Gusman

+0

@Gusman非常有趣!當我右鍵點擊它時,有一個選項顯示「添加假組件」,我使用的是.NET-3.5 btw – Bassie

+0

,這是因爲沒有找到該文件,VS詢問您是否要創建僞裝配。嘗試糾正第一個錯誤。 – Gusman

回答

0

我找到了我正在尋找的答案here

基本上,我有我的參考,從

C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll

改變

C:\Program Files(x86)\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll

出於某種原因,這徹底解決了我所有的問題 - 我不知道有什麼區別介於這兩個程序集之間,並且非常感謝對此的深入瞭解。