简体   繁体   中英

Process.Start() is not executing batch file through c# code

I am using below code to execute a batch file through c# console application

ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = "Batch File Path";
            psi.WorkingDirectory = "Folder path where batch file is located";
            psi.UseShellExecute = false;


            //User under which batch file has to be executed
            using (new Impersonator("UserName", "DomainName", "Password"))
            {
                Process.Start(psi);
            }

This code works fine if I run console application from same machine with different user. But it fails to execute batch file, if I call it from remote machine using powershell with delegation on. Below is the powershell command to call exe from remote machine.

$pw = convertto-securestring -AsPlainText -Force -String Password
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "UserName",$pw
$opt = new-pssessionoption -OperationTimeOut 7200000 -OutputBufferingMode Drop
$sessions = New-PSSession -ComputerName ServerName -sessionOption $opt -credential $cred -Authentication CredSSP
Invoke-Command -session $sessions -ScriptBlock {"Path of exe on remote machine" | out-null} 
Remove-PSSession -ComputerName ServerName

Note: Please note that delegation is setup between machines.

Please advice. Thanks

您是否尝试过使用/ c运行cmd?

processInfo = new ProcessStartInfo("cmd.exe", "/c exec.bat");

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