2011-07-25 45 views
0

我正在爲我的工作寫一個Web服務(WCF),我正在尋找一種方法來在其他機器上按需運行腳本 。
我們得到了我們從RDC連接的機器,並且我想從另一個C#程序運行它的腳本 。
另外,我似乎無法找到一種方法來在C#的另一臺機器上運行可執行文件。在C機器上運行腳本從其他機器#

回答

1

爲什麼你不能找到.net框架,讓你運行另一臺機器上的可執行文件是一部分原因因爲沒有一個。

如果您想在遠程計算機上運行可執行文件,那麼您可能對PsExec(由Micrososft發佈的Sysinternals工具)感興趣。

+0

PsExec非常適合在遠程運行程序,但我似乎無法找到運行腳本的方法。 例如,我想在服務器上運行命令「md c:\ temp」。 我該怎麼做?因爲PsExec只運行該進程 – Ben2307

2

這是可能的通過C#中使用WMI(見http://www.codeproject.com/KB/cs/Remote_Process_using_WMI_.aspx)或通過使用命令行http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx ...但它是你平時不應該做 - 它創造了幾個安全問題處理...

編輯 - WMI使用用戶/ PW:
connectionoptions給你提供用戶名+密碼的可能性 - 看http://msdn.microsoft.com/en-us/library/system.management.connectionoptions.aspx

+0

從來不知道你可以使用WMI來做到這一點 - 很好的發現。 – Justin

+0

使用WMI如何連接使用用戶和密碼? – Ben2307

+0

看到我的編輯 – Yahia

0

我知道這是一篇舊文章,但我想補充一點,當然可以在沒有PsExec的情況下運行遠程可執行文件,而許多病毒軟件標記爲有問題。另外大多數系統管理員不希望在他們的Web服務器上使用PsExec。而且,通過映射驅動器或UNC運行遠程可執行文件並不意味着您已安裝了許可證,因此可能會失敗(或運行演示版),具體取決於軟件。

關鍵是在WCF服務中包裝System.Diagnostics.Process步驟。下面是一個部分示例...

{ 
// Define a service contract. 
[ServiceContract(Namespace = "http://MyDataServices.foo.com")] 
public interface IDataService 
{ 
    [OperationContract] 
    bool ProcessStatTransfer(MemoryStream inputCommands, string inputName); 

} 

// Implement the IDataService service contract in a service class. 
public class DataService : IDataService 
{ 

    // Implement the IDataService methods. 
    public bool ProcessStatTransfer(MemoryStream inputCommands, string inputName) 
    { 
     try 
     { 
      // StatTransferLocation is C:\Program Files\StatTransfer12-64\st.exe 
      // on the machine that hosts the service 
      string m_stattransfer_loc = ConfigurationManager.AppSettings["StatTransferLocation"].ToString(); 
      string m_stattransfer_file = ConfigurationManager.AppSettings["CommandFiles"].ToString() + inputName; 

      using (FileStream m_fsfile = new FileStream(m_stattransfer_file, FileMode.Create, FileAccess.Write)) 
      { 
       inputCommands.WriteTo(m_fsfile); 
      } 

      ProcessStartInfo processInfo = new ProcessStartInfo("\"" + m_stattransfer_loc + "\""); 

      processInfo.Arguments = "\"" + m_stattransfer_file + "\""; 
      processInfo.UseShellExecute = false; 
      processInfo.ErrorDialog = false; 
      processInfo.CreateNoWindow = true; 

      Process batchProcess = new Process(); 
      batchProcess.StartInfo = processInfo; 
      batchProcess.Start(); 

      return true; 

     } 
     catch 
     { 

      return false; 

     } 

    } 
..... 

然後,您添加一個服務引用並調用該方法。沒有映射,PsExec或WMI。這是一個純粹的C#解決方案。

相關問題