2012-05-09 52 views
0

我是新來的C#編碼,請幫助我下面,謝謝你.. !!!c#在Windows 2008 64位上運行命令遠程

我嘗試以下操作:

我開發小C#應用程序來執行遠程服務器上的批處理文件,下面是我的代碼,我的大多數服務器是Windows 2008的64位,如果我RDP連接到服務器,我可以執行該批處理文件沒有任何錯誤,但是當我嘗試通過下面的代碼執行時,它不起作用,沒有發生異常,但沒有結果。

我的批處理文件包含以下命令:

@ECHO off 
echo Running Remote Commands 
date/t 
time /t 
COPY "\\xt0022\I$\abc\RemoteProcess\testcopy.bat" D:\ab\ 
date/t 
time /t 

-

try 
{ 
    string remotemachine = "Server1"; 

    object[] theProcessToRun = { "D:\\ab\\test2.bat" }; 
    ConnectionOptions theConnection = new ConnectionOptions(); 
    theConnection.Impersonation = ImpersonationLevel.Impersonate; 
    theConnection.EnablePrivileges = true; 
    ManagementScope theScope = new ManagementScope("\\\\" + remoteMachine + "\\root\\cimv2", theConnection); 
    ManagementClass theClass = new ManagementClass(theScope, new ManagementPath("Win32_Process"), new ObjectGetOptions()); 
    theClass.InvokeMethod("Create", theProcessToRun); 

} 
catch (Exception ex) 
{ 

} 

如果我調試的代碼,它顯示 「推導=功能評價超時。」

我必須用RUNAS運行這個.. ..?如果是..任何人都可以幫助我使用這些代碼或方法..?

謝謝大家..!

回答

1

你看過ProcessStartInfo嗎?

ProcessStartInfo startInfo = new ProcessStartInfo("PsExec.exe"); 
startInfo.Arguments = @"\\<computername -u <username> -p <password> -i c:\\test.bat"; 
Process.Start(startInfo); 

這是CodeProject.com上的一個artice,它使用WMI創建一個遠程過程,就像你在做的一樣。完整的源代碼可用。

http://www.codeproject.com/Articles/31113/Create-a-Remote-Process-using-WMI-in-C

這可能是一個愚蠢的問題,但什麼是你的超時值設置爲?如果太小,那可能是原因。

+0

是的,我已經嘗試了相同的代碼,這裏是另一種方式來看看這個問題。如果我執行包含本地路徑的批處理文件,它工作正常,但如果我有遠程文件位置來複制它,它會失敗:-(也TimeOut未設置,我使用任何默認值。我必須明確設置更多的超時時間..? – user1265448

+0

我的意思是如果我在批處理文件中這樣做它工作正常:複製「C:\ temp \ abc.txt」D:\ ab \ – user1265448

+0

因此,它是超時獲取文件從遠程位置。我沒有遇到過使用遠程命令的問題,抱歉。 –

相關問題