简体   繁体   中英

How to execute a bat file on remote server?

I have two physical machine. The first machine want to execute a bat file on the second machine. I create a share location on the second server and tried to run it from the first machine and it does not work. The bat file can run on the local machine properly.

This is an example of code inside the bat file. It is just a bat to create file.

echo. 2>EmptyFile.txt

This is the code that I use to execute bat file.

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();
}

I want to know how can I run bat file on remote server?

You can execute commands on remote machines using PsExec . I use it to execute batch files and pass parameters to them.

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

Or, if you need to specify the domain:

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

psexec is the only way know how to execute a program on a remote computer. 知道如何在远程计算机上执行程序的唯一方法。

This can be easily done from command prompt or bat file

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

For help type:

wmic /?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM