Reputation: 1034
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
Upvotes: 0
Views: 1160
Reputation: 149
Have you tried to ask the cmd run with "/c"?
processInfo = new ProcessStartInfo("cmd.exe", "/c exec.bat");
Upvotes: 1