2012-10-01 105 views
0

我有兩臺物理機器。第一臺機器想要在第二臺機器上執行一個bat文件。我在第二臺服務器上創建一個共享位置,並嘗試從第一臺機器運行它,但它不起作用。 bat文件可以正確運行在本地機器上。如何在遠程服務器上執行bat文件?

這是bat文件內部代碼的一個例子。這只是一個創建文件的蝙蝠。

echo. 2>EmptyFile.txt 

這是我用來執行bat文件的代碼。

private static void ExecuteBatFile() 
{ 
    System.Diagnostics.Process proc = new System.Diagnostics.Process(); 
    System.Security.SecureString ssPassword = new System.Security.SecureString(); 

    proc.StartInfo.UseShellExecute = false; 
    proc.StartInfo.FileName = @"\\my_remote_server\my_bat.bat"; 
    proc.StartInfo.Domain = "my_domain"; 
    proc.StartInfo.UserName = "my_username"; 

    Console.Write("Enter your password: "); 
    string password = Console.ReadLine(); 

    for (int x = 0; x < password.Length; x++) 
    { 
     ssPassword.AppendChar(password[x]); 
    } 

    proc.StartInfo.Password = ssPassword; 
    proc.Start(); 
} 

我想知道如何在遠程服務器上運行bat文件?

+0

http://www.codeproject.com/Articles/31113/Create-a-遠程過程使用WMI-in-C –

回答

3

您可以使用PsExec在遠程機器上執行命令。我用它來執行批處理文件並將參數傳遞給它們。

+0

我們需要在哪裏安裝psexec?在物理上存在文件的服務器上,還是在運行代碼以發送命令的服務器上? – Sak

+0

「只需將PsExec複製到您的可執行文件路徑。」 - >您在哪裏運行代碼 – marapet

2
Set WshShell = WScript.CreateObject("WScript.Shell") 
WshShell.Run "psexec \\my_remote_server -u my_username -p " & password & " my_bat.bat" 

或者,如果您需要到指定域:

Set WshShell = WScript.CreateObject("WScript.Shell") 
WshShell.Run "psexec \\my_remote_server -u my_domain\my_username -p " & password & " my_bat.bat" 

psexec是我知道如何在遠程計算機上執行程序的唯一途徑

+0

我們需要在哪裏安裝psexec?在物理上存在文件的服務器上,還是在運行代碼以發送命令的服務器上? – Sak

1

這可以從命令提示符或批處理文件

wmic /node:MachineName> process call create "cmd.exe c:\Test\Test.bat" 

輕鬆完成有關幫助類型:

wmic /? 
相關問題